NPE With custom config file

Discussion in 'Plugin Development' started by bowlerguy66, Nov 25, 2015.

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

    bowlerguy66

    I am trying to use a custom config file. But when I do it this time, it doesn't work. This is the method I do to load all of my config files and it works fine for everything else.

    Code:
        File gemFile;
        FileConfiguration gFile;
       
        public FileConfiguration getGems() {   
            return gFile;
        }
       
        public void saveGems() {
            try {
                gFile.save(gemFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
       
        public void loadFiles() {
           
            gemFile = new File(plugin.getDataFolder(), "gems.yml");
    
            if(gemFile.exists()) {
                try {
                    gFile = YamlConfiguration.loadConfiguration(gemFile);
                    plugin.getServer().getConsoleSender().sendMessage(symbol + ChatColor.GREEN + "Gems loaded properly");           
                } catch(Exception e) {
                    plugin.getServer().getConsoleSender().sendMessage(symbol + ChatColor.RED + "Gems could not be loaded!");           
                    e.printStackTrace();
                }
            } else {
                try {
                    gemFile.createNewFile();
                    gFile = YamlConfiguration.loadConfiguration(gemFile);
                    plugin.getServer().getConsoleSender().sendMessage(symbol + ChatColor.GREEN + "Gems created and loaded");           
                } catch(Exception e) {
                    plugin.getServer().getConsoleSender().sendMessage(symbol + ChatColor.RED + "Gems could not be created!");           
                    e.printStackTrace();               
                }
            }
           
        }
    When I use the method in the onEnable it says Gems loaded properly, but when I try to use it as a config I get an NPE.

    Code:
    Code:
        public boolean hasGems(Player p) {
           
            String uuid = p.getUniqueId().toString();
           
            if(getGems() == null) {
                Bukkit.broadcastMessage("Gems is null");
            }
           
            if(getGems().contains("Players." + uuid + ".Gems")) { // The line that causes NPE, the getGems() returns the config file (Makes things easier to read)
                return true;
            } else {
                return false;
            }
           
        }
    
    NPE:
    Code:
    [13:31:43 ERROR]: Could not pass event PlayerInteractEvent to Prison v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:227) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at me.bowlerguy66.prison.GemsManager.hasGems(GemsManager.java:89) ~[?:?]
        at me.bowlerguy66.prison.IdInventory.onInteract(IdInventory.java:49) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[craftbukkit.jar:git-Spigot-fdc1440-53fac9f]
        ... 17 more
    
     
  2. Offline

    Zombie_Striker

    Ahh, youth. I remember a time before I learned to always null check EVERYTHING.
     
    Last edited: Nov 25, 2015
  3. Offline

    bowlerguy66

    Yes, I know how to read stack traces. I did null check and it is returning null. I just have no clue why
     
  4. Offline

    Zombie_Striker

    @bowlerguy66
    Just updated previous post to make it a little more clear what I was talking about.
     
  5. Offline

    bowlerguy66

    Ok, I changed it up a bit. But it still isn't working

    Code:
        public boolean hasGems(Player p) {
          
            String uuid = p.getUniqueId().toString();
          
            if(getGems() == null) {
                Bukkit.broadcastMessage("Gems is null");
            }
          
            if(getGems().get("Players." + uuid + ".Gems") == null) {
                return false;
            }
          
            if(getGems().contains("Players." + uuid + ".Gems")) {
                return true;
            } else {
                return false;
            }
          
        }
    EDIT: I think the NPE is coming from the getGems() method, therefore the config is null. So what did I do wrong with the loadFiles() method?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 25, 2015
  6. Offline

    Zombie_Striker

    BTW, Only bump every 24 hours.
     
Thread Status:
Not open for further replies.

Share This Page