1.8.8 Getting ItemStack from yaml config returns NPE

Discussion in 'Spigot Plugin Development' started by Jacrispys, Jan 13, 2021 at 10:59 PM.

  1. Title: I have no idea why it is returning this I'll post class code and yaml.
    Code (Java):
    public class Main extends JavaPlugin implements Listener {

        private static Plugin plugin;



        @Override
        public void onEnable() {

            plugin = this;
            new EditUI(this);
            new OptionsGUI(this);
            new UIclicks(this);
            if(getConfig().get("MyGUI.Items") instanceof ConfigurationSection) {
                getServer().getConsoleSender().sendMessage((ChatColor.AQUA + "[BanGUI] Config Successfully Loaded"));
                ConfigurationSection items = getConfig().getConfigurationSection("MyGUI.Items");
                Map<String, Object> itemStackMap = new HashMap<String, Object>();
                for(String key : items.getKeys(false)){
                    try {
                        if (!(key.contains("meta")) && (!(key.contains("slot")))) {
                            itemStackMap.put(key, items.getItemStack(key));
                            getServer().getConsoleSender().sendMessage("this is the key" + key);
                            getServer().getConsoleSender().sendMessage("itemstack is " + items.getItemStack(key));
                            if(key != null) {
                                ItemStack im = ItemStack.deserialize(itemStackMap);
                                EditUI.inv1.setItem((Integer) items.get("MyGUI.Items." + key), im);
                            } else return;
                        } else {
                            getServer().getConsoleSender().sendMessage("not slot key: " + key);
                        }
                    }
                    catch(NullPointerException e) {
                        System.out.println(Arrays.toString(e.getStackTrace()));
                    }

                    String itemType = getConfig().getString("MyGUI.Items." + key + ".type");
                    String displayName = getConfig().getString("MyGUI.Items." + key + ".display-name");
                    String lore = getConfig().getString("MyGUI.Items." + key + ".lore");
                }
                saveDefaultConfig();
                // add code to get items from config
            } else {
                getConfig().set("MyGUI.Items", "");
                getServer().getConsoleSender().sendMessage((ChatColor.AQUA + "Config Sections not present creating..."));
                saveDefaultConfig();
                saveConfig();
            }




        }
        @Override
        public void onDisable() {
            saveDefaultConfig();

        }

        public static Plugin getPlugin() {
            return plugin;
        }


       



    }
     
    Code (YAML):
    MyGUI:
      Items
    :
        '1'
    :
          ==
    : org.bukkit.inventory.ItemStack
          type
    : STONE
          meta
    :
            ==
    : ItemMeta
            meta-type
    : UNSPECIFIC
            display-name
    : test
            lore
    :
           - 3 Year
     
     
  2. Unfortunatly I did do that I serlalize the item in a different class when its created and then after that when the server restarts the item is deserialized and put into the inventory but when it tries to put the item it returns npe
     
  3. Which line is throwing an NPE? If this line is working, you should just use that method instead of manually deserializing it.
    Code (Java):
    getServer().getConsoleSender().sendMessage("itemstack is " + items.getItemStack(key));
     
  4. Could also just do System.out.println?
     
  5. I tried that but then the line adding the item to the inventory throws NPE right now the line deserilaizing throws the NPE ill post the stack trace


    [org.bukkit.inventory.ItemStack.<init>(ItemStack.java:94), org.bukkit.inventory.ItemStack.deserialize(ItemStack.java:536), me.Jacrispys.BanGui.Main.onEnable(Main.java:42), org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321), org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340), org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405), org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357), org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317), net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414), net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378), net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333), net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263), net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525), java.base/java.lang.Thread.run(Thread.java:832)]
     
  6. Yeah that would work too. I just pasted in what OP had.

    Save the ItemStack you get from
    Code (Java):
    items.getItemStack(key)
    and use that instead of calling the deserialize method.
     
  7. Code (Java):
    if(key != null) {
                                ItemStack im = items.getItemStack(key);
                                EditUI.inv1.setItem((Integer) items.get("MyGUI.Items." + key), im);
                            } else return;
    new code like you said and got the same error, the line that errored was

    Code (Java):
    EditUI.inv1.setItem((Integer) items.get("MyGUI.Items." + key), im);
     
  8. You're casting the entire ItemStack to an int. I think you just want to parse `key` as an int (I'm assuming that will be the inventory slot).
     
    • Agree Agree x 2
  9. yeah I noticed the same thing after I lastt posted and changed the code to
    Code (Java):
     if(Integer.valueOf(key) != null) {
                                ItemStack im = items.getItemStack(key);
                                EditUI.inv1.setItem(Integer.parseInt(key), im);
                            } else return;
    and now I get the NPE with this stack trace
    [me.Jacrispys.BanGui.Main.onEnable(Main.java:38), org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321), org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340), org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405), org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357), org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317), net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414), net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378), net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333), net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263), net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525), java.base/java.lang.Thread.run(Thread.java:832)]

    line 38 is the one that sets the item
     
  10. did you check the (im) is valid or not?
     
  11. like to check if its not null? or what do you mean, in one line of code I print the string value of IM to make sure it exists and it does
     
  12. can you show us the full of the error please?
     
  13. that was the full stacktrace but ill show the error without catching NPE+

    [10:47:01 ERROR]: Error occurred while enabling BanGui vAlpha-0.1 (Is it up to date?)
    java.lang.NullPointerException: Cannot invoke "org.bukkit.inventory.Inventory.setItem(int, org.bukkit.inventory.ItemStack)" because "me.Jacrispys.BanGui.Commands.EditUI.inv1" is null
    at me.Jacrispys.BanGui.Main.onEnable(Main.java:38) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[TestServer.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [TestServer.jar:git-Spigot-db6de12-18fbb24]
    at java.base/java.lang.Thread.run(Thread.java:832) [?:?]
     
  14. oh I think the GUI hasen't been initialized by the time the plugin enables
    let me fix that

    how would I take
    Code (Java):
    public void GUI(Player p) {

            inventory_name = Utils.chat("&cBan List");

            Inventory inv1 = Bukkit.createInventory(null, inv_rows, inventory_name);


            inv1.setItem(slot - 1, item);
            p.openInventory(inv1);
            }
    and run it in Main class without static abuse?
     
  15. Now I get it you got this error because you didn't defined the inventory.
    upload_2021-1-14_21-53-16.png
     
  16. yeah but how would I define it in my Main class because its in a different one?
     
  17. You should create that inventory in EditUI's constructor and make accessor methods so you can fiddle with it in your main class. Right now you're just creating new instances of those classes in onEnable without saving them. You can do something like this:
    Code (Java):
    private EditUI editUI;

    @Override
    public void onEnable() {
      ...
      editUI = new EditUI(this);
      ...
      editUI.getUI().setItem(...);
    }
    TBH, this looks like it should all be done in the UI class instead of your main class. You're already using dependency injection so you can just do these things in the constructor of EditUI instead of in onEnable.
     
    • Agree Agree x 1
  18. ok Ill fiddle with it and leave the thread open until i figure it out.