NullPointer onEnable onLoadkeys

Discussion in 'Spigot Plugin Development' started by Bladian, Jul 6, 2015.

  1. I have this error:

    Code (Text):
    [20:29:54 INFO]: [Crates] Enabling Crates v1.1
    [20:29:54 ERROR]: Error occurred while enabling Crates v1.1 (Is it up to date?)
    java.lang.NullPointerException
        at me.bladianyt.core.Core.onEnable(Core.java:44) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot.jar:git-Spigot-1646]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [spigot.jar:git-Spigot-1646]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot.jar:git-Spigot-1646]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot.jar:git-Spigot-1646]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot.jar:git-Spigot-1646]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:866) [spigot.jar:git-Spigot-1646]
        at org.bukkit.Bukkit.reload(Bukkit.java:301) [spigot.jar:git-Spigot-1646]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [spigot.jar:git-Spigot-1646]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) [spigot.jar:git-Spigot-1646]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1646]
    [20:29:54 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [20:29:54 INFO]: BladianYT: Reload complete.
    Which is on line 44 which is on this method:

    Code (Text):
    public void loadKeys() {

            Configuration configuration = KeyConfig.getInstance().getKeyConfig();

            File file = KeyConfig.getInstance().getKeyFile();

            if(file.length() != 0) {

                for (String keyLines : configuration.getKeys(false)) {
                    Material material = Material.getMaterial(configuration.getString(keyLines + "material"));
                    String name = configuration.getString(keyLines + "name");
                    List<ItemStack> itemStacks = new ArrayList<ItemStack>();
                    for (String itemLines : configuration.getConfigurationSection(keyLines).getKeys(false)) {
                        ItemStack itemStack = new ItemStack(Material.getMaterial(itemLines), 1);
                        itemStacks.add(itemStack);
                    }
                    KeyInterface keyInterface = new KeyInterface(name, material, itemStacks);
                    keyInterfaces.add(keyInterface);


                }
            }
            else Bukkit.getLogger().fine("No keys to load!");
        }



    }
     
    Don't see the problem. Any help?
     
  2. According to the stack trace, line 44 is in onEnable, not in loadKeys. Recompile the plugin.
     
  3. @BurnerDiamond - Also can you use pastebin to upload your code so it retains line numbers. You'll have to provide code from package name to the line the problem is occurring on and, Not just a snippet.
     
    • Agree Agree x 1
  4. Line 44 is the load keys method. The error is on line 44, in the onEnabled from my method:

    KeyManager.getInstance().loadKeys();

    Which is the load keys method.
     
  5. As I mentioned already, Please provide the code from package name to the end of the method where the error exists in a pastebin. Snippets don't help.

    Also, Refer to this sticky post in the future: https://www.spigotmc.org/threads/read-me-for-everyone-asking-questions-on-here.72946/
     
  6. But why do you need the whole code if the snippets are all that are in the error? 742 message on the plugin development and I've never seen this.

    http://pastebin.com/UGGa1t3a
    http://pastebin.com/L6f1vAQL
     
  7. @BurnerDiamond - You're calling a static method in the KeyManager class. Trying to get an instance variable which is null, Because you haven't initialized it. Good reason to stay away from static access when you're new to Java imo.
     
  8. Static variables are not all bad
     
  9. Not new to Java... Static is used correctly is good, I obviously used it incorrectly in this case. I know Java is an OOP language, and I never use static's unless needed. This was misused. Since I took it from my core class.

    Code (Text):
    private static Core instance;

        public static Core getInstance() {
            if (instance == null) {
                instance = new Core();
            }
            return instance;
        }

        public Core() {
            instance = this;
        }
     
  10. Never mind I misunderstood, I skipped the lesson on initialisation, what seems to be wrong? It now works because everything is static/initialized so what exactly is the problem with before?

    CODE NOW:

    Code (Text):
    package me.bladianyt.keys;

    import me.bladianyt.core.Core;
    import me.bladianyt.keys.config.KeyConfig;
    import org.bukkit.Material;
    import org.bukkit.configuration.Configuration;
    import org.bukkit.inventory.ItemStack;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * Created by BladianYT. By seeing this you have agreed to not copy
    * or use the code, which are you viewing unless you have been granted permission
    * by the developer.
    */

    public class KeyManager {

        private static KeyManager instance;

        public static KeyManager getInstance() {
            return instance;
        }

        public KeyManager() {
            instance = this;
        }

        public static KeyList getKeyInterfaces() {
            return keyInterfaces;
        }

        private static KeyList keyInterfaces = new KeyList();

        public static void loadKeys() {

            Configuration configuration = KeyConfig.getKeyConfig();
            for (String keyLines : configuration.getKeys(false)) {
                Material material = Material.getMaterial(configuration.getString(keyLines + "material"));
                String name = configuration.getString(keyLines + "name");
                List<ItemStack> itemStacks = new ArrayList<ItemStack>();
                for (String itemLines : configuration.getConfigurationSection(keyLines).getKeys(false)) {
                    ItemStack itemStack = new ItemStack(Material.getMaterial(itemLines), 1);
                    itemStacks.add(itemStack);
                }
                KeyInterface keyInterface = new KeyInterface(name, material, itemStacks);
                keyInterfaces.add(keyInterface);
            }
        }
    }
     
  11. @BurnerDiamond - Can you attach the new stack trace and both classes in a pastebin. I'll give it another look over.