2

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;
        }

    }

}
4

1 回答 1

1

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

文档建议在onEnable() 和 onDisable() 中简单地调用 getLogger(),可能是因为那时已经调用了 initialize()。

注意: 源代码说不要自己调用 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

于 2013-07-23T03:51:31.547 回答