Solved Another Null Pointer Exception

Discussion in 'Spigot Plugin Development' started by NukeDude, Mar 14, 2018.

  1. I really have to get better at predicting and fixing these errors, but this is the largest plugin I've worked on and I'm unable to catch everything. Getting the exception on the last line.
    Code (Text):
    private boolean clickOrder;
        ItemStack item = new ItemStack(Material.AIR);
        private void firstRightClick(PlayerInteractEvent e)
            Player player = e.getPlayer();
            if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
                ItemStack item = (player.getInventory().getItemInMainHand());
                if (item.getType().toString().equalsIgnoreCase(getConfig().getString("VoucherItem")))
                    if (loreChecker(item.getItemMeta().getLore()))
                        player.sendMessage(ChatColor.AQUA + "Now select a tool or weapon or some armor to apply the effect to.");
                        clickOrder = true;
        private void secondRightClick(PlayerInteractEvent e)
            Player player = e.getPlayer();
            if (clickOrder = true)
                if (item.getItemMeta().hasLore())
                    if (item.getItemMeta().getLore().contains("armor"))
    The plugin checks if you're right-clicking with a paper and takes the first line of the paper's lore and applying it to a second item. This is only a fraction of the code, but it's all that's relevant.
  2. the fun thing about exceptions is that it will tell you the exact line and reason for this exception. i see multiple places where a NPE could happen in your code.
  3. I know the line and reason, just not sure how to fix it
    at me.TheNukeDude.LoreVoucher.secondRightClick( ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
    at$1.execute( ~[spigot-1.12.2.jar:git-Spigot-4bd94dc-9ab298d]

    EDIT: alright I figured it out
    #3 NukeDude, Mar 14, 2018
    Last edited: Mar 14, 2018
  4. Un-solving this, the problem seems to still be occurring.
    Adding if(item == null) return; fixes nothing by the way.
  5. Do you mind showing us the new code?
  6. also please mark the in the error mentioned line for us
  7. You're not checking if a player even right clicked with an item in their hand..they could've just right clicked.

    Code (Text):
      if (item.getType().toString().equalsIgnoreCase(getConfig().getString("VoucherItem")))
    will never work because getType will return a material as an enum like PAPER, DIAMOND_SWORD, STONE, etc.. not the name of the item.
  8. Unless you know what the VoucherItem string is, you can't say that. Not saying that you're wrong but you aren't right as well since you're unaware of what that string may be.

    As for NukeDude:
    1. Why in the world do you have multiple events doing the same thing?
    2. What line did the error occur?
    3. What does the method loreChecker do? (Do you mind showing us?)
    4. Not something important but:
    Code (Java):

    /*( <- this is useless */ player.getInventory().getItemInMainHand() /* ) <- this is also useless.*/;
  9. I would agree normally if voucheritem was a variable but its just a string that has "VoucherItem"...
  10. and? you know that toString returns the enum name as String (not variable) and config.getString("VoucherItem2) also returns a string written in the config?
  11. Since the string is "Item" and not "Name" assuming that the config path contains an item's material name is safer. Unfortunately assumptions are not encouraged in this case.
  12. wow im stupid.. i didnt see the config.getString.... [​IMG]
  13. Forgot to mark this as solved :)
    Fixed it on my own

Share This Page