Solved NullPointerException?

Discussion in 'Plugin Development' started by Blockhead7360, Jul 5, 2015.

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

    Blockhead7360

    Hello!

    I am creating a plugin, and I am getting a null pointer exception on line 38 (line 3 below)

    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            if (map.get(e.getWhoClicked().getName())){
                e.setCancelled(true);
               
            }
            if (hash.get(e.getWhoClicked().getName())){
                ItemStack i = e.getCurrentItem();
                List<ItemStack> list = (List<ItemStack>) getConfig().getList("mailbox." + maplel.get(e.getWhoClicked().getName()));
                list.remove(i);
                getConfig().set("mailbox." + maplel.get(e.getWhoClicked().getName()), list);
                saveConfig();
               
            }
            else if (map.get(e.getWhoClicked().getName()) == false){
                return;
            }
           
            else return;
           
        }
        

    Please help, thanks!
     
  2. Line 3 is "map.get" right? Is "map" null? I'm guessing "map"'s value is of type Boolean, so if the player who clicked isn't in the Map, it'd return null, throwing a NullPointerException. Check if they're in the Map first before getting the value.
     
  3. Offline

    Blockhead7360

  4. No problem, please mark this thread as solved using the tools at the top of this thread. And if you're feeling gr8, like my post m8!
     
    Blockhead7360 likes this.
  5. Offline

    Blockhead7360

    @KingFaris11 your right! I forgot to mark the post as solved and I forgot to like your post ;)

    @KingFaris11 Could you also help with this:

    I am getting a NullPointerException on lore.add("horray...");

    Code:
                    ItemMeta meta = i.getItemMeta();
                    List<String> lore = meta.getLore();
                    lore.add("horray it worked");
                    meta.setLore(lore);
                    i.setItemMeta(meta);
    EDIT: Actually... Just a sec.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
    KingFaris11 likes this.
  6. Because initially, when the lore is empty (no lore exists), it is null and you're invoking the add method on a null instance. You should do:
    Code:
    List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
    What this does is, if the item meta has a lore, get the current list of lores, but if it doesn't, return a new ArrayList.

    By the way, you should check if the item meta is not null before getting the lores, and continuing your code.

    I didn't actually look at your code fully, I only looked at line 3. There are a few things wrong with this method.

    Firstly, you didn't check if "hash" contains the player username.

    Secondly, you can use: if (!map.get(e.getWhoClicked().getName()) instead of the == false bit. This is basic Java knowledge...

    Thirdly, you didn't check if the current item is not null. This can throw a NullPointerException.

    There are a few more mistakes, but I'm off now, you'll come across them yourself most likely, anyway see ya =)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  7. Offline

    Blockhead7360

    @KingFaris11
    1. What do you mean?
    2. I know. I actually used that way - "(!Boolean)" - in my previous code. I just did it that way randomly ;)
    3. Okay, got it

    Thank you for pointing out my mistakes. Luckily, I know how to read the console (what happened and where), so I can fix it or get help to fix it.

    Oh and also, you previously said
    I have never heard of using this before, so I wanted to learn this to make sure I know how to use it in other plugins.

    I think I understand it though, but I'm not sure. If you were to do:

    Code:
                    String d = meta.hasDisplayName() ? meta.getDisplayName() : null;
    
    Does that mean that if the meta has a display name, then "d" would be the display name. Otherwise it will be null?
     
  8. Offline

    WesJD

    @Blockhead7360 The quote just makes the lore your making either have the current lore in it(if it has one) or just a blank one. For your d, yes, that is how it works.

    I recommend learning some more Java before getting into Bukkit though. Otherwise, it's going to be quite hard to understand.
     
  9. Offline

    Blockhead7360

    @WesJD

    Got it! I already started that. Like, it took me a while to understand for loops, except now I do, and I use them all the time!

    Thanks, and I will continue learning more Java. ;)
     
  10. I believe the "? :" is the bit you're confused about. It's called a ternary operator and acts as an "if" statement in one line. I could have done:
    Code:
    List<String> lore;
    if (meta.hasLore()) {
        lore = meta.getLore();
    } else {
        lore = new ArrayList<>();
    }
    
    Which does the EXACT same thing, but it takes up more lines and in my opinion, looks ugly relative to the ? : statement.

    The format of how to use ? : goes like this
    <condition> ? <whattoreturniftrue> : <whattoreturniffalse>

    More information: http://java.about.com/b/2009/06/06/java-term-of-the-week-ternary-operator.htm

    What I meant for point 1 was, you did hash.get(playerName) but didn't check if they're in the map first.
     
  11. Offline

    Blockhead7360

    @KingFaris11 okay I got it! I read the link and your post, and now I understand how that works.

    List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<String>();

    I now know that the line above is basically saying IF (as in the ternary operator) the meta has a lore, then get the lore it already has. ELSE (as in the ":"), create a new ArrayList.

    This worked for me, thank you!
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page