Null Pointer From Getting Materials

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

  1. Code (Text):
    [00:15:29 ERROR]: Error occurred while enabling Crates v1.1 (Is it up to date?)
    java.lang.NullPointerException
        at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:68) ~[spigot.jar:git-Spigot-1646]
        at me.bladianyt.keys.KeyManager.loadKeys(KeyManager.java:44) ~[?:?]
        at me.bladianyt.core.Core.onEnable(Core.java:37) ~[?:?]
        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 org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(CraftServer.java:753) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:326) [spigot.jar:git-Spigot-1646]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:290) [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]
    And the config is like this:

    Default:
    material: STONE
    name: Default
    items: STONE


    Any help?
     
  2. JustThiemo

    JustThiemo Previously TrinityDEV

    We are not magicians we cant see whats wrong without your code
     
  3. I am an idiot for not posting the code, though I pasted it in.

    Here you go on pastebin.

    http://pastebin.com/d3DxWqxE
     
  4. sothatsit

    Patron

    Material.getMaterial is probably returning null because it can't find the material. Add a check if the material is null and see if that fixes it.
     
  5. From what I can tell, it looks like you forgot a few "."s. I think you mean keyLines + ".name", not keyLines + "name". Anyway, to check this, try doing Bukkit.getPluginManager().getPlugin("your plugin name").getLogger().info(itemLines); before the line with the error (44), just to verify that you are obtaining the string from the config file correctly.

    EDIT: It would help if you pastebin'd your entire config.yml file.
     
    • Agree Agree x 2
  6. It's a key.yml so that is everything. Yeah I forgot the dots testing it now.
     
  7. sothatsit

    Patron

    It is also good practice to check if it is a valid material before creating the itemstack. That way your plugin doesn't crash when the config is wrong.
     
  8. New error:

    for (String itemLines : configuration.getConfigurationSection(keyLines + ".items").getKeys(false)) {

    Code:

    Code (Text):
    [00:46:44 ERROR]: Error occurred while enabling Crates v1.1 (Is it up to date?)
    java.lang.NullPointerException
        at me.bladianyt.keys.KeyManager.loadKeys(KeyManager.java:43) ~[?:?]
        at me.bladianyt.core.Core.onEnable(Core.java:37) ~[?:?]
        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]
     
     
  9. I'm going to add the check's and the validating messages now.
     
  10. Try it with item ID's and if that doesnt work then just make sure that line is in the config when you are trying to access it, I had a case one time that my config was skipping random lines when im copying defaults etc... which was causing me many errors
     
  11. Id's are now no longer used, so I would like to keep it, with the name so it doesn't break.
     
  12. Print out what Material it is actually getting first, to make sure you are actually grabbing the correct part of the your config.
     
  13. Yep it returns stone.
     
  14. Just read your code:

    Code (Text):
    Material material = Material.getMaterial(configuration.getString(keyLines + "material"));
    Should that be:

    Code (Text):
    Material material = Material.getMaterial(configuration.getString(keyLines + ".material"));
    Put a . before material?
     
  15. I stated before that I changed it. Here is the new code:

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

    import me.bladianyt.core.Core;
    import me.bladianyt.keys.config.KeyConfig;
    import org.bukkit.Bukkit;
    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>();
                ItemStack itemStack = new ItemStack(Material.getMaterial(keyLines + ".items"), 1);
                Bukkit.broadcastMessage(itemStack.toString());
                itemStacks.add(itemStack);
                KeyInterface keyInterface = new KeyInterface(name, material, itemStacks);
                keyInterfaces.add(keyInterface);
            }
        }
    }
     
  16. Can u show the config.yml copied from one that has been loaded onto a test server? It might just be a spelling mistake or miss typing of something, hence the npe otherwise try .toUpperCase() and try getting the material by just typing "STONE" in yourself
     
  17. Found the solution by myself but thanks for everyones help.

    Not going to tell you what I did, maybe you can spot what I changed :p.

    Code (Text):
    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 + ".items").getKeys(false)) {
                    ItemStack itemStack = new ItemStack(Material.getMaterial(configuration.getString(keyLines + ".items." + itemLines)), 1);
                    itemStacks.add(itemStack);
                }
                KeyInterface keyInterface = new KeyInterface(name, material, itemStacks);
                keyInterfaces.add(keyInterface);
            }
        }