Nullpointer exception when interact

Discussion in 'Spigot Plugin Development' started by R3XET, Nov 25, 2019.

  1. shooter is null, getiteminhand is null, getitemmeta is null, getdisplayname is null, or config is null
     
  2. There's a number of things that could be null when this code runs.
    ItemStack#getItemMeta can return null if the player is not holding an item.
    ItemMeta#getDisplayName can also return null if the item the player is holding does not have a custom display name.
    You'll have to make sure that neither of these are null before you compare the item's custom display name to something.

    Since you're always getting an NPE, it's likely that config or config#getString is null.
    Could you please post the relevant code?
     
  3. Full class: https://hastebin.com/raqileribu.java
     
  4. getiteminhand is null, getitemmeta is null, getdisplayname is null, or config is null
    print item and config
     
  5. So, do I have to add "&& if getiteminhand != null && ect" to the line 62?
     
  6. just because you have a text file doesnt mean it isnt null
    idk. check and see whats null first. see why its behaving the way that it is. learn of its behavior instead of blindly null checking everything
     
  7. So, what should I do?
     
  8. Yes, I did it.
     
  9. i told you. print item and config. simple system#out#println(item), system#out#println(config) and see what they are
     
  10. Ok, sorry, I didn't see it
     
  11. getString isnt returning null. it isnt even getting that far. if it were, his format method would call the NPE, not the event method
     
  12. I v'e printed config and items, but they're not null
     
  13. does printing the item print its meta as well? i would think it would see if the meta or the displayname of the meta is null
    the only time a meta can be null is if the item is air, so as long as you arent checking air only the displayname can be null
     
  14. it prints the item, quantity and itemmeta
    Example: ItemStack{AIR x 0}
     
  15. Strahan

    Benefactor

    Whenever I am dealing with things that can be null, I just break it down and check each tier. Makes the code longer (vertically) but not as long horizontally. I prefer it that way. Like instead of if (shooter.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(CC.translate(config.getString("switcheregg.name")))) { I'd typically do:
    Code (Text):
    ItemStack i = shooter.getInventory().getItemInMainHand();
    if (!i.hasItemMeta) return;

    ItemMeta im = i.getItemMeta();
    if (!im.hasDisplayName) return;

    String eggName = config.getString("switcheregg.name");
    if (eggName == null) return;

    if (!im.getDisplayName().equalsIgnoreCase(CC.translate(eggName))) return;
    Alternatively, if the name not matching is not a fail condition I may pass a default value to getString like String eggName = config.getString("switcheregg.name", "Unnamed"); as this would prevent an NPE because if the config is missing that node, it sets "Unnamed" as the return instead of the null default.
     
    • Winner Winner x 1
  16. I'll try tomorrow, thanks for the complete explanation.
     
  17. Thank you so much, all worked