Solved NullPointerException Problem

Discussion in 'Plugin Development' started by DividedByZero, Feb 7, 2014.

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

    DividedByZero

    Hey i am having this issue with a null pointer exception. It has to do with the commands file and the SetHub file. Here is the error.
    Code:
    [19:12:53 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pvpp
    ' in plugin PvpParkour v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    6) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:542) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:932) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
            at Commands.SetHub.setHub(SetHub.java:12) ~[?:?]
            at Commands.Commands.onCommand(Commands.java:52) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Source Code
     
  2. Offline

    xTigerRebornx

    DividedByZero
    Code:
    Commands commands = new Commands(this);
    When you are doing this, the keyword 'this' is being used when your plugin isn't loaded, so its giving a NPE
    Move the commands = new Commands(this); inside of your onEnable()
     
  3. Offline

    DividedByZero

    Alright its fixed but could you explain why this happens for the future. Im going to learn from this.
     
  4. Offline

    xTigerRebornx

    DividedByZero Basically, since this is outside of your onEnable(), you are passing an instance of your Plugin that has no data received from Bukkit (I.E. Config), therefor when trying to refrence that data, it is null. Putting it in your onEnable() allows it to use a instance that has the proper data.

    At least, that is how I see it :p
     
  5. Offline

    DividedByZero

    Alright i did that and got the same error. I though i fixed it but i didnt export it. I just updated my git hub. It has somthing to do with the private Plugin plugin; in both Commands.java and the SetHub.java files. xTigerRebornx
     
  6. Offline

    xTigerRebornx

    DividedByZero Okay, so you are making the same mistake you did in your main class
    Code:
    HubApi hubapi = new HubApi(plugin);
        Hub hub = new Hub(plugin);
        SetHub sethub = new SetHub(plugin);
    These lines are outside of the constructor for Commands, so they are refrencing a null value for Plugin. Do the same thing you did with the first problem (hubapi = new HubApi(plugin); etc,etc) after you've given Plugin a value
     
  7. Offline

    DividedByZero

    Ok thank you sooo much. I actually learned somthing from this too!.
     
Thread Status:
Not open for further replies.

Share This Page