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.

    Command
    Code (Text):

    arena.addSpawn(user.getPlayer().getLocation());
    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.getSpawns().add(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());
            this.plugin.saveArenasFile();
        }
    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(PluginCommand.java:46) ~[spigot.jar:git-Spigot-c5146ba-01930e2]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-c5146ba-01930e2]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown 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(MinecraftServer.java:718) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-c5146ba-01930e2]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at com.REMOVED.oitc.games.arenas.Arena.addSpawn(Arena.java:56) ~[?:?]
        at com.REMOVED.oitc.commands.CommandOITC.onCommand(CommandOITC.java:152) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[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):
    Arenas:
      '1':
        Lobby:
          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