I'm a noob so... (aka NullPointerExceptions and me.)

Discussion in 'Plugin Development' started by FaceValue, Jun 10, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    FaceValue

    Ok, I'm pretty sure this was covered in one of the "Warning your plugins will break" posts, but I couldn't decipher what the fix was meant to be :S
    I'm relatively new to Java, so I don't get a lot of what I've read, but I'd appreciate some help with this...

    When I attempt to start my test server with the initial version of my plugin, my log looks like this:

    Show Spoiler
    Code:
    2012-06-10 16:35:44 [INFO] Starting minecraft server version 1.2.5
    2012-06-10 16:35:44 [INFO] Loading properties
    2012-06-10 16:35:44 [INFO] Starting Minecraft server on *:2248
    2012-06-10 16:35:44 [INFO] This server is running CraftBukkit version git-Bukkit-1.2.5-R3.0-b2203jnks (MC: 1.2.5) (Implementing API version 1.2.5-R3.0)
    2012-06-10 16:35:44 [SEVERE] Could not load 'plugins\RacesV0.1.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:149)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:213)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:189)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:166)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:432)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at org.bukkit.plugin.PluginLogger.<init>(PluginLogger.java:22)
        at org.bukkit.plugin.java.JavaPlugin.getLogger(JavaPlugin.java:359)
        at uk.co.agentface.main.<init>(main.java:16)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:145)
        ... 8 more
    


    I get the general gist of this report, but I'm not sure how it relates to my actual code. Ignore the constructor, I copy/pasted that in after reading the 'Oops I broke your plugin' topic.. My main.java looks like this:

    Show Spoiler
    Code:
    package uk.co.agentface;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import com.sun.corba.se.spi.activation.Server;
    import com.sun.java.util.jar.pack.Package.File;
     
     
    public class main extends JavaPlugin {
        public main(PluginLoader pluginLoader, Server instance,
                PluginDescriptionFile desc, File folder, File plugin,
                ClassLoader cLoader) {
            super(pluginLoader, instance, desc, folder, plugin, cLoader);
        }  
        Logger raceslogger = this.getLogger(); //Line 16: apparently the line where it all goes wrong :S
        Boolean isEnabled;
        Boolean isRunning;
        private RacesCommandExecutor CommandExecutor;
        public Map<Player, String> PlayerRaces = new HashMap<Player, String>();
        public void onEnable(){
            if (isEnabled == true){
                getServer().getPluginManager().registerEvents(new PluginListener(this), this);
                CommandExecutor = new RacesCommandExecutor(this);
                getCommand("races").setExecutor(CommandExecutor);
                raceslogger.info("Plugin initialised");
                try{
                    PlayerRaces = (HashMap<Player, String>)SLAPI.load("Races/playerdata.bin");
                } catch (Exception e){
                    raceslogger.info("Races failed to load PlayerRaces HashMap.");
                    System.out.println("Exception : "+ e.getMessage());
                }
                raceslogger.info("Player data loaded.");
                isRunning = true;
                raceslogger.info("Races started up successfully.");
            }
            else{
                raceslogger.info("Races has been disabled in config.");
            }
        }
       
        public void onDisable(){
            try{
                SLAPI.save(PlayerRaces, "Races/playerdata.bin");
            } catch(Exception e){
                raceslogger.info("Races failed to save PlayerRaces HashMap.");
                System.out.println("Exception : "+ e.getMessage());
            }
            isRunning = false;
            raceslogger.info("Races has successfully shut down.");
        }
        public boolean restart(){
            if(isRunning){
                this.onDisable();
                this.onEnable();
                raceslogger.info("Races has successfully rebooted.");
                return true;
            } else {
                return false;
            }
        }
    }
    
     
  2. Offline

    McLuke500

    public final Logger logger = Logger.getLogger("Minecraft");

    Its ment to be that and also I use System.out.print() to output to console and that's what I see other people using so best use that.
     
  3. Well, this.getLogger() is totally fine and might be better since you already have the plugin prefix applied to it, so you don't need to add it all the time yourself.

    The actual problem is that you call it before the onEnable where the logger isn't created for your plugin. If you call it inside the onEnable it shouldn't be null at all.

    Also you don't need a constructor anymore.
     
  4. I personally wouldn't use this.getLogger.

    Just import it statically :

    Code:java
    1.  
    2. yourclass{
    3.  
    4. Logger log = Logger.getLogger("Anything can go here ;)");
    5.  
    6. public void onEnable(){
    7.  
    8. }
    9.  
    10. }
    11.  
     
  5. Offline

    FaceValue

    Thanks all for your help. I have solved it (I think) by intialising 'Logger raceslogger;' at the start, then making 'Logger startlogger = this.getLogger();' at the top of onEnable and then equating 'raceslogger = startlogger;' at the end. It seems to be working, but is this a good way to do it?

    Also, it now throws another NullPointerException in my PluginListener class looking like this:
    EDIT: Never mind, solved that.

    It does, however, throw one at main, line 28:
    Show Spoiler
    Code:
    2012-06-11 13:30:16 [SEVERE] Error occurred while enabling Races v0.1 (Is it up to date?)
    java.lang.NullPointerException
    at uk.co.agentface.main.onEnable(main.java:28)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:215)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:256)
    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:238)
    at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:381)
    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:368)
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:197)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:432)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    

    Main.java now looks like this:
    Show Spoiler
    Code:
    package uk.co.agentface;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Logger;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin {
    Logger raceslogger;
    Boolean isEnabled = true;
    Boolean isRunning;
    private RacesCommandExecutor CommandExecutor;
    public Map<Player, Race> PlayerRaces = new HashMap<Player, Race>();
    PluginListener RacesListener;
    Races RaceLogger;
    public void onEnable(){
    if (isEnabled == true){
    RacesListener = new PluginListener(this);
    RaceLogger = new Races(this);
    Logger startlogger = this.getLogger();
    getServer().getPluginManager().registerEvents(RacesListener, this);
    CommandExecutor = new RacesCommandExecutor(this);
    The offending line>> getCommand("races").setExecutor(CommandExecutor);
    raceslogger.info("Plugin initialised");
    try{
    PlayerRaces = (HashMap<Player, Race>)SLAPI.load("Races/playerraces.bin");
    } catch (Exception e){
    raceslogger.info("Races failed to load PlayerRaces HashMap.");
    System.out.println("Exception : "+ e.getMessage());
    }
    raceslogger.info("Player data loaded.");
    raceslogger = startlogger;
    isRunning = true;
    raceslogger.info("Races started up successfully.");
    }
    else{
    raceslogger.info("Races has been disabled in config.");
    }
    }
     
    public void onDisable(){
    try{
    SLAPI.save(PlayerRaces, "Races/playerraces.bin");
    } catch(Exception e){
      raceslogger.info("Races failed to save PlayerRaces HashMap.");
      System.out.println("Exception : "+ e.getMessage());
      }
    isRunning = false;
    raceslogger.info("Races has successfully shut down.");
    }
    public boolean restart(){
    if(isRunning){
    this.onDisable();
    this.onEnable();
    raceslogger.info("Races has successfully rebooted.");
    return true;
    } else {
    return false;
    }
    }
    }
    
     
  6. You defined the command inside plugin.yml?
     
  7. Offline

    FaceValue

    *sigh*
    I have now. Thank you.
     
Thread Status:
Not open for further replies.

Share This Page