Use of persistence in a listener causes NullPointerException

Discussion in 'Plugin Development' started by dkabot, Jan 14, 2012.

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

    dkabot

    Listeners just aren't my thing, huh? (The only people who will get that will be those who saw this thread.)

    Anyway, this time the issue is as stated above: I get a NullPointerException when trying to use Persistence in a listener. If it's in onCommand(), the exact same code works just fine.
    I have not tested this with the DB having any data, mainly because all DB interactions in the plugin this is for are through listeners.
    Rather than use code tags, I'll just link to GitHub.
    Thanks for any help in advance.

    EDIT: Here's a stack trace. As you can see, it points to line 24 which is where persistence is used.
    Code:
    2012-01-14 21:37:07 [SEVERE] Could not pass event SIGN_CHANGE to RSPassword
    java.lang.NullPointerException
    	at com.dkabot.RSPassword.RSPBlockListener.onSignChange(RSPBlockListener.java:24)
    	at org.bukkit.plugin.java.JavaPluginLoader$36.execute(JavaPluginLoader.java:514)
    	at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    	at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:340)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1079)
    	at net.minecraft.server.Packet130UpdateSign.a(SourceFile:41)
    	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:96)
    	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    	at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:534)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:432)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
     
  2. Offline

    Ranzdo

    It would be easier to help if you also parsed the stack trace.
    But I looked at you code and found that:

    Code:
    public class RSPBlockListener extends BlockListener {
        public RSPassword plugin;
    "RSPassword plugin" is never set to anything and therefore null. You can fix it by adding
    Code:
    plistener.plugin = this;
    blistener.plugin = this;
    to your onEnable() method.

    However people will probably yell at me since constructors is a more popular choice, however you code as you choose.
     
  3. Offline

    dkabot

    To my knowledge, plugin is set as RSPassword. It is just so you can do plugin.getDatabase() (example) rather than RSPassword.getDatabase().
    Also, your explanation would not give any clues as to why the code works in onCommand() but not in a listener.
     
  4. Offline

    Ranzdo

    I suggest that you read up on the diffrences between references and objects in java. But to answer your question, why it works in the onCommand is because you have:

    Code:
    private RSPassword plugin;
    public Command(RSPassword plugin) {
        this.plugin = plugin;
    }
    and in the onEnable() you have this:

    Code:
    Com = new Command(this);
    Here you set the plugin var to the plugin object. You need to do the same in the listeners.

    Code:
    PlayerListener plistener = new RSPlayerListener(this);
    BlockListener blistener = new RSPBlockListener(this);
    Code:
    public  RSPlayerListener (RSPassword plugin) {
        this.plugin = plugin;
    }
    Code:
    public  RSPBlockListener (RSPassword plugin) {
        this.plugin = plugin;
    }
    I hope that clear things up :)
     
  5. Offline

    dkabot

    2 simple fails about listeners in 1 day... I guess I'll shoot for breaking the world record. DX
    Thank you for pointing this out to me, now I can finish this plugin.
     
Thread Status:
Not open for further replies.

Share This Page