Solved "Inventory is null" error on gui command.

Discussion in 'Spigot Plugin Development' started by xSlate, May 4, 2021.

  1. I am trying to make a GUI that when you type "/gui" it will open, however for some reason when I try to run the command it doesn't work and comes up with this error in the console:
    Code (Text):
    Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.inventory.Inventory.getType()" because "inventory" is null
            at org.bukkit.craftbukkit.v1_16_R3.inventory.CraftContainer.getNotchInventoryType(CraftContainer.java:125) ~[spigot-1.16.4.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.craftbukkit.v1_16_R3.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:298) ~[spigot-1.16.4.jar:git-Spigot-37d799b-3eb7236]
            at me.xslate.originsspigot.gui.StartGUI.onCommand(StartGUI.java:30) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.16.4.jar:git-Spigot-37d799b-3eb7236]
    I looked at the code line that it says the error is coming from, and I don't understand why the error is coming from it. If anyone could help that'd be awesome :D


    (The code, and related code of the line the error said)
    Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
         
            if (label.equalsIgnoreCase("gui")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Only a player may execute this command!");
                    return true;
                }
                Player player = (Player) sender;
                player.openInventory(inventory); // error here
                return true;
             
            }
            return false;
        }
    (Not sure if it would have anything to do with where I set the GUI slots and created the items? if it might be I can put that code too)
     
    #1 xSlate, May 4, 2021
    Last edited: May 4, 2021
  2. because you are opening an inventory "inventory" which has not been created yet, can you show the code for that as well?
     
  3. Code (Text):
    public void createInventory() {
         
            inventory = Bukkit.createInventory(null, 27, Main.c("&c&lGUI"));
         
            ItemStack item = new ItemStack(Material.PAPER);
            ItemMeta meta = item.getItemMeta();
         

            //item creation

            //Glass
            item.setType(Material.BLACK_STAINED_GLASS_PANE);
            meta.setDisplayName("");
            item.setItemMeta(meta);
            inventory.setItem(0, item);
         
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(1, item);
         
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(3, item);
         
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(5, item);
         
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(7, item);
         
            //Glass
            item.setType(Material.BLACK_STAINED_GLASS_PANE);
            meta.setDisplayName(null);
            item.setItemMeta(meta);
            inventory.setItem(8, item);
         
            //Glass
            item.setType(Material.BLACK_STAINED_GLASS_PANE);
            meta.setDisplayName(null);
            item.setItemMeta(meta);
            inventory.setItem(9, item);
         
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(13, item);
         
            //Glass
            item.setType(Material.BLACK_STAINED_GLASS_PANE);
            meta.setDisplayName(null);
            item.setItemMeta(meta);
            inventory.setItem(17, item);
         
            //Glass
            item.setType(Material.BLACK_STAINED_GLASS_PANE);
            meta.setDisplayName(null);
            item.setItemMeta(meta);
            inventory.setItem(18, item);
                 
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(19, item);
                 
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(21, item);
                 
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(23, item);
                 
            //Placeholder
            meta.setDisplayName(Main.c("&5&oPlaceholder"));
            item.setItemMeta(meta);
            inventory.setItem(25, item);
                 
            //Glass
            item.setType(Material.BLACK_STAINED_GLASS_PANE);
            meta.setDisplayName(null);
            item.setItemMeta(meta);
            inventory.setItem(27, item);
         
                 
                 
                 
        }
    is this what you meant?
     
  4. You need to run the createInventory method in the onEnable to register it.
     
  5. is this method located in your Command class? if yes then run it before you do the openInventory

    Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
       
            if (label.equalsIgnoreCase("gui")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Only a player may execute this command!");
                    return true;
                }
                createInventory();
                Player player = (Player) sender;
                player.openInventory(inventory); // error here
                return true;
           
            }
            return false;
        }
     
  6. yeah it is in my command class, I tried that and it didn't seem to work though
    as for this, I'm not exactly sure how to do this? Since I'm doing all this in another class, would I just create a constructor or something and then register it?

    EDIT: I looked at the error and changed some things and it works now, thanks for the help :D
     
    • Like Like x 1