Solved Small NullPointerException

Discussion in 'Plugin Development' started by PogoStick29, Dec 1, 2012.

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

    PogoStick29

    I fixed the problem by declaring the location once it was needed, not before. Now everything works.

    Old Post:
    Show Spoiler
    I'm making a Paintball plugin and I have a small error:

    Code:java
    1. World w;
    2. double bluex;
    3. double bluey;
    4. double bluez;
    5. double redx;
    6. double redy;
    7. double redz;
    8.  
    9. public Arena(int number) {
    10. this.w = Bukkit.getServer().getWorld(settings.getConfig().getString("arenas." + number + ".world"));
    11. this.bluex = settings.getConfig().getDouble("arenas." + number + ".blue.x");
    12. this.bluey = settings.getConfig().getDouble("arenas." + number + ".blue.y");
    13. this.bluez = settings.getConfig().getDouble("arenas." + number + ".blue.z");
    14. this.redx = settings.getConfig().getDouble("arenas." + number + ".red.x");
    15. this.redy = settings.getConfig().getDouble("arenas." + number + ".red.y");
    16. this.redz = settings.getConfig().getDouble("arenas." + number + ".red.z");
    17. }
    18.  
    19. Location redspawn = new Location(w, redx, redy, redz);
    20. Location bluespawn = new Location(w, bluex, bluey, bluez);


    when I try to use the teleport method in the Player class and the redspawn location it throws a NullPointerException.

    Here's the config:

    Code:
    #
    # This number is used by the plugin. Don't touch it!
    numberofarenas: 0
    general:
      amntofsnowballs: 20
      compassesenabled: true
      playersneededtobegin: 10
      countdowntime: 10
    arenas:
      '1':
        world: HG3
        blue:
          x: -481.89693192596064
          y: 65.0
          z: -3.471071591544767
        red:
          x: -481.9367099111598
          y: 65.0
          z: 13.067123732732089
    lobbyspawn:
      world: HG3
      x: -469.2297323088223
      y: 65.0
      z: 4.621867304303069
    Here's the lovely stack trace (sorry its upside down, MultiCraft did it :3)

    Code:
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.ServerConnection.b(SourceFile:39)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:113)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.NetworkManager.b(NetworkManager.java:290)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:858)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:903)
    01.12 21:09:59 [Server] SEVERE at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:985)
    01.12 21:09:59 [Server] SEVERE at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
    01.12 21:09:59 [Server] SEVERE at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    01.12 21:09:59 [Server] SEVERE at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.commands.CommandManager.onCommand(CommandManager.java:75)
    01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.commands.Join.onCommand(Join.java:18)
    01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.Arena.join(Arena.java:132)
    01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.Arena.assignTeam(Arena.java:125)
    01.12 21:09:59 [Server] SEVERE at org.bukkit.craftbukkit.entity.CraftEntity.teleport(CraftEntity.java:175)
    01.12 21:09:59 [Server] SEVERE at org.bukkit.craftbukkit.entity.CraftPlayer.teleport(CraftPlayer.java:377)
    01.12 21:09:59 [Server] SEVERE java.lang.NullPointerException
    Assume that settings.getConfig() returns the plugin's config. I know it works because the spawn setting command works.
     
  2. Offline

    ron975

    Code:java
    1. 01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.commands.CommandManager.onCommand(CommandManager.java:75)
    2. 01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.commands.Join.onCommand(Join.java:18)
    3. 01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.Arena.join(Arena.java:132)
    4. 01.12 21:09:59 [Server] SEVERE at me.pogostick29.skyball.Arena.assignTeam(Arena.java:125)


    Line 75 of CommandManager is where you want to look, also line 18 of Join.java and lines 132 and 125 of Arena.java.
    Something in those lines, or referring to those lines are causing the NPE. Maybe post full source?

    Also, use
    Code:
    [syntax=java]
    ...
    [/syntax]
    
    It allows for line numbering and syntax highlighting
     
  3. Offline

    PogoStick29

    The problem is that when the Join method in the Arena class tries to teleport a player, but one of the values in the location is null.
     
  4. Offline

    nisovin

    I would assume the world is null. The world you're trying to access is not loaded, so Bukkit.getWorld() returns null.
     
    CeramicTitan likes this.
  5. Offline

    PogoStick29

    I am sure that the world is loaded.
     
  6. Offline

    nisovin

    So check if it's null right after you get it.
     
Thread Status:
Not open for further replies.

Share This Page