Null.. but it's not null!

Discussion in 'Plugin Development' started by chaseoes, May 19, 2012.

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

    chaseoes

    So this line is throwing a NullPointerException:
    Code:java
    1.  
    2. if (plugin.getConfig().getStringList("bans.griefing").contains("chaseoes")) {
    3.  

    Now my first thought would be that my name isn't in "bans.griefing" of my config, or that it's empty, or something like that.. but a quick debugging command is returning that my name is indeed there and that I can grab the values just fine:
    Code:java
    1.  
    2. public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
    3. if (cmnd.getName().equalsIgnoreCase("bans")) {
    4. Player player = (Player) cs;
    5. List<String> bans = getConfig().getStringList(
    6. "bans.griefing");
    7. player.sendMessage("Banned Players:");
    8. for (String bansStr : bans) {
    9. player.sendMessage(bansStr);
    10. }
    11. return true;
    12. }
    13. }
    14.  

    This is confusing - I asked around on the IRC and nobody there could figure it out too.

    That first line is somehow giving me a NullPointerException.. but grabbing the same value with my debugging code (the 2nd set of code) isn't.
     
  2. Offline

    Deleted user

    Full error please.
     
  3. Offline

    r0306

    chaseoes
    Replace 'plugin' with 'this'
     
  4. Offline

    chaseoes

    "Cannot use this in a static context"
    It needs to stay static unfortunately.

    Code:
    2012-05-19 19:43:14 [SEVERE] Could not pass event PlayerLoginEvent to MyPlugin
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:303)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
        at net.minecraft.server.ServerConfigurationManager.attemptLogin(ServerConfigurationManager.java:227)
        at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:102)
        at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:41)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:61)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at me.chaseoes.myplugin.Util.isBanned(Util.java:174)
        at me.chaseoes.myplugin.BlockListener.onPlayerLoginEvent(BlockListener.java:162)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301)
        ... 9 more
    
    Line 174 being the one I gave in the OP.
     
  5. Offline

    r0306

    Nvm.
     
  6. Offline

    Deleted user

    Can I get line 162 as well please?
     
  7. Offline

    chaseoes

    162:
    Code:java
    1. if (Util.isBanned(player)) {

    Which calls:
    Code:java
    1. public static boolean isBanned(Player pl) {
    2. if (plugin.getConfig().getStringList("bans.griefing").contains("chaseoes")) { // Line 174
    3. return true;
    4. } else {
    5. return false;
    6. }
    7. }
     
  8. Offline

    Deleted user

    Dunno then...
     
  9. Offline

    VeryBIgCorp

    Did you initialize your plugin variable in your constructor?
     
    r0306 likes this.
  10. Offline

    Iron_Crystal

    Is it possible that bans.griefing in your config is not a list of Strings?
     
  11. Offline

    r0306

  12. Offline

    russjr08

    Is your method isBanned in another class other than the main one?

    And we have an IRC Channel??!!!
     
  13. Offline

    chaseoes

    Hmm?
    I'm using:
    Code:java
    1. public static MyPlugin plugin;
    2. public Util(MyPlugin plugin) {
    3. Util.plugin = plugin;
    4. }


    Well it has to be.. because my debugging command displays it fine (see 2nd set of code in OP).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  14. Offline

    r0306

    Try making that private (MyPlugin). You want as much hidden variables as you can to prevent conflicts. Remove the static declaration. You don't need it.
    Code:
    private MyPlugin plugin;
     
  15. Offline

    chaseoes

    If I make it private then the Util.plugin part gives me the error "Cannot make a static reference to the non-static field Util.plugin", and offers to chance the modifier of 'plugin' to 'static'.
     
  16. Offline

    r0306

    chaseoes
    Change the code to this:
    Code:
    private MyPlugin plugin;
    public Util(MyPlugin plugin) {
    this.plugin = plugin;
    }
     
  17. Offline

    Sagacious_Zed Bukkit Docs

    chaseoes
    make everything non static, and if it says you cannot make a static reference get rid of that statement too.
     
  18. Offline

    chaseoes

    Still "Cannot make a static reference to the non-static field plugin".
     
  19. Offline

    r0306

    chaseoes
    Did you change Util to 'this'?
     
  20. Offline

    chaseoes

    What do you mean? I used the code you just gave.
     
  21. Offline

    r0306

    chaseoes
    Make sure you changed
    Code:
    Util.plugin = plugin;
    to
    Code:
    this.plugin = plugin;
    because if you don't that will cause the exact same error as you just described.
     
  22. But you're never calling this constructor, do you?

    //EDIT: But as others told: Make everything non static. Static in bukkit is bad anyway (memory leaks with /reload)... ;)
     
    Iron_Crystal and VeryBIgCorp like this.
  23. Offline

    chaseoes

    If I make everything non-static it screws with my code, and I don't see how that'll help this issue?
     
  24. Offline

    r0306

    chaseoes
    If it's telling you to make it static, then something went wrong with your code. Can you maybe upload your stuff onto github or post the main class plus the listener classes?
     
  25. Offline

    Sagacious_Zed Bukkit Docs

    If you made everything non-static, you WILL need to restructure your code.
     
  26. That's probably cause you don't use the constructors you define. Example:
    Static:
    MyClass.myFunction();
    not static:
    myClass.myFunction();
    of course myClass has to be initialized:
    MyClass myClass = new MyClass(this);
    and the new MyClass(this) is where you call the constructor.
    You static / non-static mix had a big problem: You where calling the function which needed a variable initialized in the constructor, but you never called the constructor, so it never initialized the variable, so it was null... So you have to call the constructor, but as you have to call it why not save the reference to the instance it returns and use this instead of the static way?
     
  27. chaseoes
    Most likely your "plugin" variable is null... but there's a verry easy way to figure this out by printing all variables' values, but you didn't debug properly because you've gone in a totally diffferent class and you've unknowingly ommited using "plugin", so next time print every variable's value starting with the verry first one.

    Are you sure about that ? :-?
    I personally never had memory leaks after I seriously used "reload" countless times when testing my plugin which has alot of static stuff.
     
  28. Offline

    xpansive

    I'd say a general rule in java is to avoid static code with everything but utiliy classes. There's always a non static way to do it and it usually works better as well.
     
  29. Offline

    r0306

    I've always made my hashmaps static in order to call them up from my other classes. Not sure if this is the correct way to do it though.
     
Thread Status:
Not open for further replies.

Share This Page