2

I have been getting the following java.lang.NullPointerException when my plugin is being enabled. I don't really see the problem.

Error:

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

    }

}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Piper McCorkle
  • 1,044
  • 13
  • 27
  • If I read the source/error correctly, the problem line is `console.info("Disabling the TARDIS plugin...");`. The only thing that could be `null` on that code line is `console`. BTW - what is with the 'almost every other blank line'? Most of those blank lines do little or nothing to help people understand the code flow - so they are just wasted screen space. – Andrew Thompson Jul 22 '13 at 21:46
  • To add more stated by @AndrewThompson You have `getLogger()` method which is not seems to be defined in your code, suppose to return `Logger` instance. – Smit Jul 22 '13 at 21:47
  • @AndrewThompson, that's how I used to code, now I have stopped coding like that. – Piper McCorkle Nov 30 '13 at 21:17
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Unihedron Oct 27 '14 at 08:16

1 Answers1

1

The NullPointerException is from the "getLogger()" call. The parent class "JavaPlugin" has to have initialize() called before the logger exists. Since you are getting the logger at instantiation time rather than after initialize() is called, the parent class returns null.

The documentation suggested simply calling getLogger() inside your onEnable() and onDisable(), likely because initialize() has been called by then.

Note: The source code say to NOT call initialize() yourself!

See:

  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

Carl Mastrangelo
  • 5,970
  • 1
  • 28
  • 37