启动我的Bukkit插件时出现NullPointerException

时间:2013-07-22 21:43:31

标签: java nullpointerexception minecraft bukkit bukkit-vault

启用插件后,我收到了以下java.lang.NullPointerException。我真的没有看到这个问题。

错误:

java.lang.NullPointerException
at tk.mypalsgaming.TARDIScraft.TARDIScraft.onDisable(TARDIScraft.java:31)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:401)
at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:394)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.disablePlugins(CraftServer.java:281)
at net.minecraft.server.v1_6_R2.MinecraftServer.stop(MinecraftServer.java:349)
at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:445)
at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)

plugin.yml

name: TARDIScraft
main: tk.mypalsgaming.TARDIScraft.TARDIScraft
version: 0.0.1
depend: [Vault]
commands:
   tardis:
      description: TARDIS Command Block and Admin Command
      usage: /<command> <TARDIS command> [parameters]
      permission: TARDIScraft.admin
      permission-message: You are not a TARDIS Admin, so you do not have access to this command.

TARDIScraft.java

package tk.mypalsgaming.TARDIScraft;

import java.util.logging.Logger;

import net.milkbowl.vault.permission.Permission;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public class TARDIScraft extends JavaPlugin {

    Logger console = getLogger();

    @Override
    public void onEnable() {

        console.info("Enabling the TARDIS plugin...");

        // TODO: onEnable code

    }

    @Override
    public void onDisable() {

        console.info("Disabling the TARDIS plugin...");

        // TODO: onDisable code

    }

    public static Permission permission = null;

    @SuppressWarnings("unused")
    private boolean setupPermissions()
    {
        RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
        if (permissionProvider != null) {
            permission = permissionProvider.getProvider();
        }
        return (permission != null);
    }

    public void onPlayerJoin(PlayerJoinEvent evt) {

        Player player = evt.getPlayer();

        if ( player.hasPermission("TARDIScraft.admin") ) {

            console.info("Admin " + player.getName() + " has joined the game.");

        }

    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if ( cmd.getName().equalsIgnoreCase("tardis") ) {

            boolean senderIsPlayer;

            if ( sender instanceof Player ) {

                senderIsPlayer = true;

            } else senderIsPlayer = false;

            // TODO: tardis Command

            if ( args[0].equalsIgnoreCase("admin") ) {

                Player playerToAdmin = Bukkit.getPlayer(args[1]);
                if ( playerToAdmin != null ) {

                    permission.playerAdd(playerToAdmin, "TARDIScraft.admin");

                }

            }

            return true;

        } else {

            return false;
        }

    }

}

1 个答案:

答案 0 :(得分:1)

NullPointerException来自“getLogger()”调用。在记录器存在之前,父类“JavaPlugin”必须调用initialize()。由于您在实例化时获取记录器而不是在调用initialize()之后,父类返回null。

documentation建议只在你的onEnable()和onDisable()中调用getLogger(),这可能是因为那时调用了initialize()。

注意:源代码说 NOT 自己调用initialize()!

请参阅:

  1. https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/plugin/java/JavaPlugin.java#L246

  2. http://wiki.bukkit.org/Plugin_Tutorial#Logging_a_message