Null Pointer Exception, Why is that here.

Discussion in 'Plugin Development' started by dbaum102, Dec 16, 2015.

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

    dbaum102

    I do not see anything null let me know if you catch something in this code I have not.
    The error thrown is on line 99 and does not reach the e.setcanceled(true);
    Error line: deeze.setDisplayName(servername);
    Code:
    @EventHandler
        public void onInvetoryClick(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
            if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Server Selector")) {
                return;
            }
            if (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR || !e.getCurrentItem().hasItemMeta()) {
                player.closeInventory();
                return;
            }
            for (int i = 0; i <= this.getConfig().getInt("Items"); i++) {
                String servername = this.getConfig().getString("Item" + i);
                int serverMaterial = this.getConfig().getInt(servername + "Material");
                Material m = Material.getMaterial(serverMaterial);
                ItemStack items = new ItemStack(m);
                ItemMeta deeze = items.getItemMeta();
                deeze.setDisplayName(servername);
                List<String> lore = new ArrayList<>();
                lore.add(ChatColor.GREEN + "ONLINE");
                deeze.setLore(lore);
                items.setItemMeta(deeze);
                if (e.getCurrentItem().getType() == m) {
                    e.setCancelled(true);
                    player.performCommand(this.getConfig().getString(servername + "Command"));
                    break;
                }
            }
    
        }
     
  2. Offline

    adam753

    getItemMeta() can be null. Check if it's null.
     
  3. Offline

    dbaum102

    This is confusing how would I create an item meta if I have nothing to reference it to?
    Like : ItemMeta deeze = items.getItemMeta();
     
    Last edited: Dec 16, 2015
  4. Offline

    adam753

    @dbaum102
    That's fine but then you would check if deeze == null.
     
  5. Offline

    dbaum102

    So
    Code:
    if(deeze == null){
        deeze.setDisplayName(ChatColor.GREEN+ server);
    }
     
  6. Offline

    567legodude

    Do you know what a null is? deeze can only be null inside of the if, so you can't call any of its methods there.
     
  7. Offline

    dbaum102

    oops im dumb my bad.
    How would I make it not null?
     
  8. Offline

    567legodude

    @dbaum102 Can you log e.getAction().toString() before the error happens, and post what it says?
     
  9. Offline

    adam753

  10. Offline

    Gorbit99

    servername can be null too, try to do a printline with servername in it
     
  11. Offline

    Mrs. bwfctower

    It cannot. First, a primitive is never null, and servername is a primitive (int). Secondly, get #getInt(String path) method returns 0 or the defined default value if the path doesn't exist.
     
  12. Offline

    mcdorli

    Is this bungeecord or lilypadMc BTW?
     
  13. Offline

    dbaum102

    It just performs command it shouldnt require any plugins
     
  14. Offline

    boomboompower

    Code:
    if (deeze != null) {
      deeze.setDisplayName(ChatColor.GREEN + server);
    }
     
  15. @Mrs. bwfctower
    Isn't servername a String? Strings can be null, they're not primitives :) They're an array of chars.
    Code:
    String servername = this.getConfig().getString("Item" + i);
     
  16. Offline

    Mrs. bwfctower

    Yes, my apologies. Combined that line and the next (though it was an int).
     
    BreezerFly likes this.
Thread Status:
Not open for further replies.

Share This Page