Error in Getting World From Config

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

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

    PogoStick29

    Code:java
    1. public Arena(int number) {
    2. this.w = Bukkit.getServer().getWorld(settings.getConfig().getString("arenas." + number + ".world"));
    3. this.bluex = settings.getConfig().getDouble("arenas." + number + ".blue.x");
    4. this.bluey = settings.getConfig().getDouble("arenas." + number + ".blue.y");
    5. this.bluez = settings.getConfig().getDouble("arenas." + number + ".blue.z");
    6. this.redx = settings.getConfig().getDouble("arenas." + number + ".red.x");
    7. this.redy = settings.getConfig().getDouble("arenas." + number + ".red.y");
    8. this.redz = settings.getConfig().getDouble("arenas." + number + ".red.z");
    9. }


    I get a NullPointerException on the World line.

    Code:
    03.12 17:02:26 [Server] INFO at me.pogostick29.skyball.Arena.<init>(Arena.java:34)
    03.12 17:02:26 [Server] INFO at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:794)
    03.12 17:02:26 [Server] INFO Caused by: java.lang.NullPointerException
    
    And here's the config:

    Code:
    #
    # This number is used by the plugin. Don't touch it!
    numberofarenas: 1
    general:
      amntofsnowballs: 20
      compassesenabled: true
      playersneededtobegin: 10
      countdowntime: 10
    arenas:
      '1':
        world: HG3
        red:
          x: -481.98378981778393
          y: 65.0
          z: -3.4770826273233766
        blue:
          x: -482.1985902381851
          y: 65.0
          z: 13.08312643016882
    lobbyspawn:
      world: HG3
      x: -488.28058628065054
      y: 65.0
      z: 4.863518905960773
    
    UPDATE: If I do
    Code:java
    1. this.w = Bukkit.getServer().getWorld("HG3");

    Then it works.

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

    fireblast709

    arenas.1.world does not exist, so it returns null (before you say it does, '1' exists, not 1. Otherwise there would be no error ;3)
     
  3. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı Retired Staff

    Add debug code and see what happens.
     
    devilquak likes this.
  4. Offline

    PogoStick29

    Like what?

    All the other code above works except or the World line, so the 1 vs. '1' isn't a problem.

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

    mbaxter ʇıʞʞnq ɐ sɐɥ ı Retired Staff

    debug code like echoing what conf value you're searching for and the result of that query.
     
  6. Offline

    PogoStick29

    Ok, thx.
     
  7. Offline

    fireblast709

    PogoStick29
    Code:java
    1. public World getWorld(String name)
    2. {
    3. return worlds.get(name.toLowerCase()); // Line 794
    4. }
    Except if you want to say that worlds is null, name.toLowerCase() is the only part that can cause the NPE here. Which means "arenas.1.world" is not set in your config, and the getString() returns null
     
  8. Offline

    PogoStick29

    Last edited by a moderator: May 30, 2016
  9. Offline

    fireblast709

    Post the code including the System.out.println()
    Now print the number aswell. It might be that it is not 1
     
  10. Offline

    PogoStick29

    What do you mean by Print the Number?
     
  11. Offline

    fireblast709

    System.out.println("arenas." + number + ".world");
     
  12. Offline

    PogoStick29

    Ah, ok, one sec.

    fireblast709
    Code:
    Code:java
    1.  
    2. public Arena(int number) {
    3. Bukkit.getServer().getLogger().info("arenas." + number + ".world");
    4. this.w = Bukkit.getServer().getWorld(settings.getConfig().getString("arenas." + number + ".world"));
    5. this.bluex = settings.getConfig().getDouble("arenas." + number + ".blue.x");
    6. this.bluey = settings.getConfig().getDouble("arenas." + number + ".blue.y");
    7. this.bluez = settings.getConfig().getDouble("arenas." + number + ".blue.z");
    8. this.redx = settings.getConfig().getDouble("arenas." + number + ".red.x");
    9. this.redy = settings.getConfig().getDouble("arenas." + number + ".red.y");
    10. this.redz = settings.getConfig().getDouble("arenas." + number + ".red.z");
    11. }
    12.  

    What I get in the console:
    Code:
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.ServerConnection.b(SourceFile:39)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:113)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.NetworkManager.b(NetworkManager.java:290)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:858)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:903)
    03.12 19:36:18 [Server] SEVERE at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:985)
    03.12 19:36:18 [Server] SEVERE at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
    03.12 19:36:18 [Server] SEVERE at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    03.12 19:36:18 [Server] SEVERE at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    03.12 19:36:18 [Server] SEVERE at me.pogostick29.skyball.commands.CommandManager.onCommand(CommandManager.java:77)
    03.12 19:36:18 [Server] SEVERE at me.pogostick29.skyball.commands.Join.onCommand(Join.java:18)
    03.12 19:36:18 [Server] SEVERE at me.pogostick29.skyball.ArenaManager.getAllPlayers(ArenaManager.java:44)
    03.12 19:36:18 [Server] SEVERE at me.pogostick29.skyball.ArenaManager.getArenas(ArenaManager.java:37)
    03.12 19:36:18 [Server] SEVERE at me.pogostick29.skyball.Arena.<init>(Arena.java:34)
    03.12 19:36:18 [Server] SEVERE at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:794)
    03.12 19:36:18 [Server] SEVERE java.lang.NullPointerException
    03.12 19:36:18 [Server] INFO ... 16 more
    03.12 19:36:18 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
    03.12 19:36:18 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:601)
    03.12 19:36:18 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    03.12 19:36:18 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    03.12 19:36:18 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    03.12 19:36:18 [Server] INFO at me.pogostick29.skyballball.listeners.PlayerCommand.onPlayerCommand(PlayerCommand.java:14)
    03.12 19:36:18 [Server] INFO at me.pogostick29.skyball.ArenaManager.getAllPlayers(ArenaManager.java:44)
    03.12 19:36:18 [Server] INFO at me.pogostick29.skyball.ArenaManager.getArenas(ArenaManager.java:37)
    03.12 19:36:18 [Server] INFO at me.pogostick29.skyball.Arena.<init>(Arena.java:34)
    03.12 19:36:18 [Server] INFO at org.bukkit.craftbukkit.CraftServer.getWorld(CraftServer.java:794)
    03.12 19:36:18 [Server] INFO Caused by: java.lang.NullPointerException
    03.12 19:36:18 [Server] INFO at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.ServerConnection.b(SourceFile:39)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:113)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.NetworkManager.b(NetworkManager.java:290)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:858)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:903)
    03.12 19:36:18 [Server] INFO at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:977)
    03.12 19:36:18 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    03.12 19:36:18 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    03.12 19:36:18 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    03.12 19:36:18 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
    03.12 19:36:18 [Server] INFO org.bukkit.event.EventException
    03.12 19:36:18 [Server] SEVERE Could not pass event PlayerCommandPreprocessEvent to Skyball v1.0
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  13. Offline

    fireblast709

    me.pogostick29.skyball.ArenaManager.getArenas(ArenaManager.java:37)​
    What is on ArenaManager.java line 37?​
     
  14. Offline

    PogoStick29

    fireblast709

    Code:java
    1.  
    2. public List<Arena> getArenas() {
    3. List<Arena> arenas = new ArrayList<Arena>();
    4. for (int number = 0; number < settings.getConfig().getInt("numberofarenas"); number++) {
    5. arenas.add(new Arena(number));
    6. }
    7. return arenas;
    8. }
    9.  


    The line is
    > arenas.add(new Arena(number));
     
  15. Offline

    fireblast709

    Problem found. your indices here go from 0 to 0 (as the numberofarenas = 1). So the only area you create is arenas.0.world, which does not exist. Then you are passing null into getWorld(), which causes the NPE.
    The fix:
    Code:java
    1. public List<Arena> getArenas()
    2. {
    3. List<Arena> arenas = new ArrayList<Arena>();
    4. for (int number = 1; number <= settings.getConfig().getInt("numberofarenas"); number++)
    5. {
    6. arenas.add(new Arena(number));
    7. }
    8. return arenas;
    9. }
     
  16. Offline

    xXSniperzzXx_SD

    Code:
    World world = Bukkit.getServer().getWorld(plugin.getConfig().getString("Word Name"));
            
     
  17. Offline

    fireblast709

  18. Offline

    xXSniperzzXx_SD

    Well he can't get the world from the config, and that will get the world name, and then make it a world so you can actually use it.
    Code:
    Location Loc = new Location(world, Integer.valueOf(floc[1]), Integer.valueOf(floc[2]),Integer.valueOf(floc[3]));
            
    Is how i use it


    This is how i fixed it when i got this problem
     
  19. Offline

    fireblast709

    xXSniperzzXx_SD he could not get it because his code that was creating the Arena objects was wrong. (which I already fixed)
     
  20. Offline

    xXSniperzzXx_SD

    Oh, i guess i should probably read other people's responses instead of just answering :p
     
  21. Offline

    PogoStick29

    Problem still not solved so...

    P ^
    M |
    U |
    B |
     
  22. Offline

    fireblast709

    PogoStick29
     
  23. Offline

    PogoStick29

    fireblast709
    Current code:
    Code:java
    1.  
    2. public List<Arena> getArenas() {
    3. List<Arena> arenas = new ArrayList<Arena>();
    4. for (int number = 1; number <= settings.getConfig().getInt("numberofarenas"); number++) {
    5. arenas.add(new Arena(number));
    6. }
    7. return arenas;
    8. }
    9.  
     
  24. Offline

    fireblast709

    What is the error now?
     
  25. Offline

    PogoStick29

    The same one. A NullPointerException on getting the world when creating a new instance of the Arena class.

    If you happen to have Skype, we can discuss this on there. Also, if you use eclipse and download Saros, you can look at and edit the code in real time.

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

    fireblast709

    Don't use Eclipse, I use Netbeans. Also, then the world does not exist, check the indices of the for loop
     
  27. Offline

    PogoStick29

    Thanks for your help, I'll try and fix it. :)
     
Thread Status:
Not open for further replies.

Share This Page