Development Assistance Why does this throw a NullPointerException?

Discussion in 'Plugin Help/Development/Requests' started by Vamure, Jan 10, 2015.

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

    Vamure

    Code:
        public int WORLD_BORDER = this.plugin.getConfig().getInt("WorldBorder");
    The int isn't empty in the config.

    20:51:08] [Server thread/ERROR]: Error occurred while enabling eBasic v2.0 (Is it up to date?)
    java.lang.NullPointerException
    at me.blowns.main.events.BorderListener.<init>(BorderListener.java:19) ~[?:?]
    19 is the snippet shown above.

    I access the int with this.WORLD_BORDER then do my checks within my player events. It doesn't work.

    But it does work if I do this.plugin.getConfig().getInt("WorldBorder") then do my checks.

    Example:
    This will work:
    Code:
    if (Math.abs(event.getBlock().getX()) > this.plugin.getConfig().getInt("WorldBorder") 
    But this won't:
    Code:
    if (Math.abs(event.getBlock().getX()) > this.WORLD_BORDER 
    Any idea?​
     
  2. Offline

    Konato_K

    plugin is null probably, anyway, can you say what "this will work but this won't" means?

    What is "won't work"?

    Edit: After checking a bit, I guess the config does not contain that element maybe?
     
  3. Offline

    nverdier

    @Konato_K No... The one that doesn't work doesn't even have 'plugin' in it. Probably WORLD_BORDER is null.
     
  4. Offline

    teej107

    @nverdier WORLD_BORDER is an int. Can't be null.
    @Vamure initialize your variable in the constructor.
     
  5. Offline

    nverdier

    @teej107 Well I meant not initialized :p
     
  6. Offline

    teej107

    @nverdier Uninitialized primitives can never be null. Uninitialized primitives have a default value like 0 for ints.
     
    Konato_K likes this.
  7. Offline

    nverdier

  8. Offline

    ColaCraft

  9. Offline

    teej107

    @ColaCraft Since when did the getInt() method return an Integer? I would read the documentation on that method to which I have conveniently made a link to.
    Solving this issue can be done by doing:
     
  10. Offline

    ColaCraft

    lol @ documentation.

    second lol @ your quote not being on the page when I posted that :p

    ggwp, m9
     
  11. Offline

    nverdier

  12. Offline

    ColaCraft

    @nverdier lol, internet :p

    At least it wasn't like extreme troll language. This thread should be marked solved soon, probably wouldn't flood the comments :rolleyes:
     
    nverdier likes this.
  13. Offline

    Vamure

    Same error.
    Code:
          public BorderListener(BasicPlugin plugin)
          {
            this.plugin = plugin;
            this.WORLD_BORDER = this.plugin.getConfig().getInt("WorldBorder");
          }
     
  14. Offline

    nverdier

    @Vamure Debug. Print out values of variables to see what's null!
     
  15. Offline

    mythbusterma

    @Vamure

    Well, it's going to be at a different line now, but I'm willing to bet that "plugin" is still null, or that value doesn't exist in the config.

    Nevermind.
     
    Last edited: Jan 11, 2015
  16. Offline

    teej107

    Will default to 0 as said in the documentation if the value doesn't exist.


    @Vamure Post updated stacktrace and the line where it is occurring. I also think that plugin is null from the info your giving us.
     
  17. Offline

    mythbusterma

    @teej107

    Good fight, teej. The only thing that can be null on that line is plugin (or config, by some convoluted nightmare code).
     
  18. Offline

    Vamure

    Code:
    [17:30:16 ERROR]: Error occurred while enabling eBasic v2.0 (Is it up to date?)
    java.lang.NullPointerException
            at me.blowns.main.events.BorderListener.<init>(BorderListener.java:12) ~
    [?:?]
            at me.blowns.main.BasicPlugin.registerEvents(BasicPlugin.java:187) ~[?:?
    ]
            at me.blowns.main.BasicPlugin.onEnable(BasicPlugin.java:106) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[s
    pigot1543.jar:git-Spigot-1543]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:329) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.jav
    a:476) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.
    java:394) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(CraftServer.java:86
    6) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot1543.jar:git-Spigot-
    1543]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:767) [spigot1543.jar:git-Spigot-1543]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(Craf
    tServer.java:753) [spigot1543.jar:git-Spigot-1543]
            at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:
    321) [spigot1543.jar:git-Spigot-1543]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    85) [spigot1543.jar:git-Spigot-1543]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot1543.jar:git-Spigot-1543]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :490) [spigot1543.jar:git-Spigot-1543]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [spigot1543.jar:git-Spigot-1543]
    Line 12: public int WORLD_BORDER = this.plugin.getConfig().getInt("WorldBorder");

    Line 187 (Main class): pm.registerEvents(new BorderListener(this), this);

    Line 106: registerEvents(); (The method I use to register my events)
     
  19. Offline

    1Rogue

    ...
    I'm gonna say that "plugin" is null. Fields are initialized before the constructor is called. But it's not easy to determine without seeing your main class and the class throwing the NPE.
     
  20. Offline

    nverdier

    @Vamure What do you get from this...
     
    mythbusterma likes this.
  21. Offline

    Konato_K

    @nverdier Mmmm... are you implying that plugin is null? :O
     
    nverdier likes this.
Thread Status:
Not open for further replies.

Share This Page