NullPointerException?

Discussion in 'Plugin Development' started by will181, Jul 20, 2014.

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

    will181

    Hello there.

    I have a config which contains a section called "prisonHomes". I am trying to loop through this section and assign the data that I retrieve in to a hashmap.

    The config should look something like this:
    Code:
    prisonHomes:
        UUID:
          x:
          y:
          z:
          world:
        UUID:
          x:
          y:
          z:
          world:
        UUID:
          x:
          y:
          z:
          world:
    This is my code:

    Code:java
    1. ConfigurationSection prisonHomes = getConfig().getConfigurationSection("prisonHomes");
    2. for(String uuid : prisonHomes.getKeys(false)) {
    3. ConfigurationSection uuidSet = prisonHomes.getConfigurationSection(uuid);
    4. double x = uuidSet.getDouble("x");
    5. double y = uuidSet.getDouble("y");
    6. double z = uuidSet.getDouble("z");
    7. World world = Bukkit.getWorld(uuidSet.getString("world"));
    8.  
    9. Location loc = new Location(world, x, y, z);
    10. this.homesPrison.put(uuid, loc);
    11. }


    My issue is that it is printing a NullpointerException when this code runs.

    Thank you for any help you can offer.
     
  2. Offline

    Pizza371

    will181
    Can you provide the error pleae? :)
    EDIT: and the line numbers
     
  3. Offline

    hankered

    Error please.
     
  4. Offline

    will181

    hankered Pizza371
    Code:
    [03:01:02] [Server thread/ERROR]: Error occurred while enabling BetterHomes v1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.will181.plugins.homes.Main.onEnable(Main.java:75) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[custom.jar:git-Spigot-1376]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324) [custom.jar:git-Spigot-1376]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [custom.jar:git-Spigot-1376]
        at org.bukkit.craftbukkit.v1_7_R2.CraftServer.loadPlugin(CraftServer.java:462) [custom.jar:git-Spigot-1376]
        at org.bukkit.craftbukkit.v1_7_R2.CraftServer.enablePlugins(CraftServer.java:380) [custom.jar:git-Spigot-1376]
        at net.minecraft.server.v1_7_R2.MinecraftServer.n(MinecraftServer.java:351) [custom.jar:git-Spigot-1376]
        at net.minecraft.server.v1_7_R2.MinecraftServer.g(MinecraftServer.java:326) [custom.jar:git-Spigot-1376]
        at net.minecraft.server.v1_7_R2.MinecraftServer.a(MinecraftServer.java:282) [custom.jar:git-Spigot-1376]
        at net.minecraft.server.v1_7_R2.DedicatedServer.init(DedicatedServer.java:186) [custom.jar:git-Spigot-1376]
        at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:440) [custom.jar:git-Spigot-1376]
        at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [custom.jar:git-Spigot-1376]
    
    75 is this line:
    Code:java
    1. for(String uuid : prisonHomes.getKeys(false))
    {
     
  5. Offline

    stonar96

    The configuration section prisonHomes is null, I think there are no values for that path in the config.yml ... ? If there are no default values in the config.yml with the path "prisonHomes.*", then always do a null check

    Code:java
    1. if (prisonHomes != null) {/*code*/}
     
  6. Offline

    will181

    stonar96 There are no default values. What you suggested still gave me an error, so in addition to that I encased it in a try catch statement and that seems to have sorted it.
     
  7. Offline

    Giraffeknee

  8. Offline

    Habbolant

    Try:
    Code:java
    1. Location loc = new Location(Bukkit.getWorld(world), x, y, z);
     
  9. Offline

    Necrodoom

    Habbolant Which will do what exactly? Get a world off an already existing world?
     
  10. Offline

    Habbolant

    "world" needs to be an world object instead of a string. (Bukkit.getWorld(String name) just returns a world with the given name.)

    His code:
    Code:java
    1. new Location(String, double, double double);

    Proper code:
    Code:java
    1. new Location(World, double, double, double);


    Source: http://jd.bukkit.org/rb/doxygen/da/dac/classorg_1_1bukkit_1_1Location.html#
     
  11. Offline

    Giraffeknee

    Habbolant
    I'm pretty sure his code gets a world correctly
     
  12. Offline

    Necrodoom

    Habbolant Go look at line 7 of his code. Seems to be a proper World object to me.
     
    Dragonphase likes this.
  13. Offline

    Habbolant

    Wow, I failed :D (I didn't read the whole code, I thought "world" is String :()
    will181 Necrodoom Sorry for confusing you :( .
     
  14. Offline

    will181

    Habbolant likes this.
Thread Status:
Not open for further replies.

Share This Page