NPE on trying to save locations

Discussion in 'Spigot Plugin Development' started by Regablith, May 27, 2015.

  1. Hello, I am trying to create multiple spawn points for my mini-game plugin but I am having an issue when trying to add a spawn point then saving it to the "arenas.yml" file.

    Code (Text):

    user.sendMessage(getPlugin().getStrings().getPrefix() + "&aSpawn point added for Arena ID: " + getPlugin().getArenaManager().getNumOfArenas());
    Arena#addSpawn Method
    (By the way: the first 2 lines in the addSpawn method work correctly. The issue is coming when I try writing to the file)
    Code (Text):
        public void addSpawn(Location location)
            this.setNumOfSpawns(this.getNumOfSpawns() + 1);
            this.plugin.arenasConfig.set("Arenas." + this.getId() + ".NumOfSpawns", this.plugin.arenasConfig.getInt("Arenas." + this.getId() + ".NumOfSpawns") + 1);
            this.plugin.arenasConfig.set("Arenas." + this.getId() + ".Spawns." + this.getNumOfSpawns() + ".World", location.getWorld().getName());
            this.plugin.arenasConfig.set("Arenas." + this.getId() + ".Spawns." + this.getNumOfSpawns() + ".X", location.getX());
            this.plugin.arenasConfig.set("Arenas." + this.getId() + ".Spawns." + this.getNumOfSpawns() + ".Y", location.getY());
            this.plugin.arenasConfig.set("Arenas." + this.getId() + ".Spawns." + this.getNumOfSpawns() + ".Z", location.getZ());
            this.plugin.arenasConfig.set("Arenas." + this.getId() + ".Spawns." + this.getNumOfSpawns() + ".Yaw", location.getYaw());
            this.plugin.arenasConfig.set("Arenas." + this.getId() + ".Spawns." + this.getNumOfSpawns() + ".Pitch", location.getPitch());
    Error in Console
    Code (Text):
    [17:04:10] [Server thread/INFO]: Rega issued server command: /oitc addspawn 1
    [17:04:10] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'oitc' in plugin ????Oitc v1.0.6-BETA
        at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-c5146ba-01930e2]
        at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot.jar:git-Spigot-c5146ba-01930e2]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand( ~[spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand( [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot.jar:git-Spigot-c5146ba-01930e2]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_45]
        at Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:60) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-c5146ba-01930e2]
        at [spigot.jar:git-Spigot-c5146ba-01930e2]
        at Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at ~[?:?]
        at com.REMOVED.oitc.commands.CommandOITC.onCommand( ~[?:?]
        at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-c5146ba-01930e2]
        ... 15 more
    Line 56
    Code (Text):
    this.plugin.arenasConfig.set("Arenas." + this.getId() + ".NumOfSpawns", this.plugin.arenasConfig.getInt("Arenas." + this.getId() + ".NumOfSpawns") + 1);
  2. What is line 56 in your Arena class?
  3. Oops, forgot to specify that.
    Code (Text):
    this.plugin.arenasConfig.set("Arenas." + this.getId() + ".NumOfSpawns", this.plugin.arenasConfig.getInt("Arenas." + this.getId() + ".NumOfSpawns") + 1);
  4. Check if there is a path called Arenas.ID.NumOfSpawns
  5. This is my arenas.yml
    Code (Text):
          World: world
          X: -468.56551973482806
          Y: 4.0
          Z: -288.33136441801497
          Yaw: 208.19998
          Pitch: 90.0
        NumOfSpawns: 0
        Spawns: []
    EDIT: Just changed it so "Spawn" does not define as a list.
  6. Okay, check if arenaConfig is set to anything, and check if this.getID() returns anything
  7. arenaConfig is set to the FileConfiguration for the arenas.yml (Which is confirmed to work) and this.getId() returns the Arena's ID as a Integer (Confirm works)
  8. Does 'this.plugin' have a value yet?
  9. Have you tried setting the value BEFORE you try to write it?

    To see if maybe the issue is trying to get the value while in the writing method?
  10. It's just defined as private Main plugin;
  11. You need to set that equal to something.

    private Main plugin = Bukkit.getServer().getPluginManager().getPlugin("MyPluginName");

    EDIT: Change Main to Plugin
    • Like Like x 1
  12. Also... in your config, you have the number listed as '1' and not 1. does getInt worked on quoted numbers?
  13. Yes it does.
  14. It does, yes.

    Thats what I thought :p

    EDIT: Thanks @BetaNyan, it worked. :)
  15. No problem! :D