Solved NullPointerException of GUI item

Discussion in 'Plugin Development' started by ToldiIII, Jul 17, 2018.

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

    ToldiIII

    So, I would have a problem as it is in the title that NullPointerException throws a bug in the console when I try to add an item to the GUI and throw this error:
    Code:
    java.lang.NullPointerException
    at hu.montlikadani.Test.Main.openMenu(Main.java:560)
    at hu.montlikadani.Test.Commands.onCommand(Commands.java:105)
    Attention! Now, I'll explain that the basics, such as command registration, plugin.yml etc. it's all right and it's okay.

    Here is the code:
    Main.java:
    Code:
    public void openGui(Player p) {
    //547        for (int i = 0; i < getConfig().getConfigurationSection("items.").getKeys(false).size(); i++) {
    //548            Inventory menu = Bukkit.getServer().createInventory(p, getConfig().getInt("gui-rows") * 9,
                     ChatColor.translateAlternateColorCodes('&', getConfig().getString("gui-name")));
    
    //551            @SuppressWarnings("deprecation")
    //552            ItemStack iStack = new ItemStack(getConfig().getInt("items.slot-" + i + ".item-id"));
    //553            ItemMeta iMeta = iStack.getItemMeta();
    //554            ArrayList<String> lore = new ArrayList<String>();
    
    //556            for (int j = 0; j < getConfig().getStringList("items.slot-" + i + ".lore").size(); j++) {
    //557                lore.add(ChatColor.translateAlternateColorCodes('&', getConfig().getStringList("items.slot-" + i + ".lore").get(j)));
                }
    
    //560            iMeta.setDisplayName(getConfig().getString("items.slot-" + i + ".name").replaceAll("&", "ยง"));
    //561            iMeta.setLore(lore);
    
    //563            iStack.setItemMeta(iMeta);
    //564            menu.setItem(i, iStack);
    
    //566            p.openInventory(menu);
            }
    }
    Commands.java (example command):
    Code:
    if (cmd.getName().equalsIgnoreCase("opengui")) {
        Player p = (Player) sender;
        plugin.openMenu(p);
    }
    Configuration:
    Code:
    gui-rows: 1
    gui-name: '&5Settings'
    items:
      slot-2:
      item-id: 345
      name: '&3Warp'
      lore:
      - '&aWarp command.'
      command: setspawn
    

    My problem is:
    When I try to open this command I will not let the GUI be released because the above console throws an error and I can not open the GUI.
     
    Last edited by a moderator: Jul 17, 2018
  2. Offline

    timtower Administrator Administrator Moderator

    @ToldiIII Does the config contain that string?
     
  3. Offline

    ToldiIII

    Yes, it's included, I did.
     
  4. Offline

    timtower Administrator Administrator Moderator

    Can you print the value before you use it in line 560?
     
  5. Offline

    ToldiIII

    Yes, when I remove the 560 line then the next issue in the console is that the lore does not appear. If lore is removed, then this error will not be displayed.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @ToldiIII Print the value before line 560.
    Never said to remove.
     
  7. Offline

    ToldiIII

    I've done it already, but nothing.
     
  8. Offline

    timtower Administrator Administrator Moderator

    Nothing as in? Does it print null? Does it give an error?
     
  9. Offline

    PretMetInternet

    Can it be that the problem is that the plugin is searching for items.slot-1?
     
  10. Offline

    ToldiIII

    It does not give an error.
     
  11. Offline

    timtower Administrator Administrator Moderator

    But what does it give?
    Print all the keys in the configurationsection using getKeys(false)
     
  12. Offline

    ToldiIII

    @PretMetInternet
    It is possible that he is looking for because I have not changed so far that there is nothing in slot 1, I took it away and that error came up, I'll try to get it done.

    @timtower
    I'm not going to print it because it's in the configuration and tell me... Why would it be needed for machine printing? From a moderator like you expect me to help, not print it because it makes no sense. If you say I export it then I've done it.
    I'm sorry for that!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jul 17, 2018
  13. Offline

    timtower Administrator Administrator Moderator

    And how can you be sure that the plugin sees it as well? That the file is there doesn't mean that the plugin reads it correctly.
    Printing things is debugging, you need that right now.
    Something is null.
    iMeta is not null because getItemMeta() generates an empty instance.
    getConfig() loads itself if not done already so that can't be null.
    So the only option is the config value at this point.
     
    johnny_boy likes this.
  14. Offline

    ToldiIII

    Okay, I'm not currently working on debugging and I will not need it. (We wore this out).
    In the configuration, the value already exists and does not succeed.
     
  15. Offline

    timtower Administrator Administrator Moderator

    @ToldiIII You are sure that it exists?
    Based on your code and posted config the for loop runs once.
    For i = 0;
    Your value in the config is slot-2
    Code search for slot-0.

    Value does not exist.
     
  16. Offline

    ToldiIII

    Well, I managed to fix it, a very simple error was that the slot in the GUI needs 0 (zero), because it will throw that error (config problem).
     
Thread Status:
Not open for further replies.

Share This Page