NullPointerException when adding data to config

Discussion in 'Plugin Help/Development/Requests' started by AcePilot10, Jul 26, 2015.

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

    AcePilot10

    So, iv'e been making a factions like MMO minigame. when I try to save the arenas in a yaml file the console throws a NullPointerException. I've tried many things, and made sure I had if statements to check if the hashmap was null, and even if the yml was null, but I just can't find the problem. Here's the save method:
    Code:
        public void saveArenas() {
            if(!ArenaManager.getManager().getArenas().isEmpty()) {
                for(String s : ArenaManager.getManager().getArenas().keySet()) {
                    int spawns = ArenaManager.getManager().getArena(s).getBases().size();
                    Arena a = ArenaManager.getManager().getArena(s);
                    for(int i = spawns; i > 0; i--) {
                        String world = a.getBases().get(i).getLocation().getWorld().getName();
                        int x = a.getBases().get(i).getLocation().getBlockX();
                        int y = a.getBases().get(i).getLocation().getBlockY();
                        int z = a.getBases().get(i).getLocation().getBlockZ();
                        arenas.set(s + ".Base" + i + ".World", world);
                        arenas.set(s + ".Base" + i + ".X", x);
                        arenas.set(s + ".Base" + i + ".Y", y);
                        arenas.set(s + ".Base" + i + ".Z", z);
                        arenas.set(s + ".Base" + i + ".Name", a.getBases().get(i).getName());
                        arenas.set(s + ".Base" + i + ".Type", a.getBases().get(i).getType());
                        arenas.set(s + ".Bases", spawns);
                        saveArenasFile();
                    }
                }
            }
    Also, here's the console.

    Code:
    [03:20:58 INFO]: [UponAnarchy] Disabling UponAnarchy v1.0
    [03:20:58 ERROR]: Error occurred while disabling UponAnarchy v1.0 (Is it up to d
    ate?)
    java.lang.NullPointerException
            at me.AcePilot10.UponAnarchy.ConfigManager.Arenas.saveArenas(Arenas.java
     
  2. Offline

    mine-care

    @AcePilot10
    Can we see the full error code? you have chopped it down to a point where it is not pointing to any class/line
     
  3. @AcePilot10
    Show us the full stacktrace indeed, but also:
    Code:
    for(int i = spawns; i > 0; i--) {
    will throw an IndexOutOfBoundsException, because you start at the size of the spawns, and if you call spawns.get(i) it will return a number outside of the spawns range. Use an incrementing loop from 0 to spawns.
     
  4. Offline

    AcePilot10

    @mine-care Here ya go :)
    Code:
    [11:59:56 INFO]: [UponAnarchy] Enabling UponAnarchy v1.0
    [11:59:56 ERROR]: Error occurred while enabling UponAnarchy v1.0 (Is it up to da
    te?)
    java.lang.NullPointerException
            at me.AcePilot10.UponAnarchy.Main.onEnable(Main.java:26) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[c
    raftbukkit.jar:git-Bukkit-2642f9b]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:332) [craftbukkit.jar:git-Bukkit-2642f9b]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:404) [craftbukkit.jar:git-Bukkit-2642f9b]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.jav
    a:341) [craftbukkit.jar:git-Bukkit-2642f9b]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.
    java:313) [craftbukkit.jar:git-Bukkit-2642f9b]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:4
    06) [craftbukkit.jar:git-Bukkit-2642f9b]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:3
    70) [craftbukkit.jar:git-Bukkit-2642f9b]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:3
    25) [craftbukkit.jar:git-Bukkit-2642f9b]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.jav
    a:235) [craftbukkit.jar:git-Bukkit-2642f9b]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :503) [craftbukkit.jar:git-Bukkit-2642f9b]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    [11:59:56 ERROR]: Permission node 'uponanarchy.admin' in plugin description file
    for UponAnarchy v1.0 is invalid
    And, @megamichiel I have changed it to
    Code:
    for(int i = 0; i  < spawns; i++) {
    
     
  5. Offline

    Monollyth

    You can use a null check for the file and create a new file if the file is null. When saving a file you could try using a "try and catch" statement. It is the way I store data in config files.
     
  6. Offline

    HeadGam3z

    What's line 26 in your Main class (in your onEnable)? Also, your plugin.yml's permission uponanarchy.admin is invalid.
     
  7. Moved to Bukkit Alternates.
     
  8. Offline

    AcePilot10

    @Monollyth I do save it like that.
    Code:
        private void saveArenasFile() {
            File f = new File(Main.plugin.getDataFolder(), "arenas.yml");
            if(!f.exists()) {
                try {
                    f.createNewFile();
                    arenas = YamlConfiguration.loadConfiguration(f);
                } catch (IOException e) {
                    Main.plugin.getLogger().severe("Error generating arenas file: " + e);
                }
            }
            arenas = YamlConfiguration.loadConfiguration(f);
        }
    @HeadGam3z line 26 is just calling the loadArenas method, which is throwing an error because the arena file has nothing in it. And the permission was just a type, it's fixed now.

    @bwfcwalshy umm, don't really understand why it was moved, but whatever you say sir.
     
  9. Offline

    HeadGam3z

    yeeeeeeeaaahhhhh, it's not saving. you're discarding all changes via loadConfiguration lol.
     
  10. Offline

    AcePilot10

    @HeadGam3z oooooooooooo so I only use loadConfiguration when creating the file?!
     
  11. @AcePilot10
    You call loadConfiguration to load the config from the file, and you call YamlConfiguration#save(File) to save it to a file.
     
Thread Status:
Not open for further replies.

Share This Page