Solved NPE when I'm checking for null?

Discussion in 'Plugin Development' started by johnny_boy, Jul 9, 2018.

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


    Here is the code, also tried replacing i with 1 but still same error. No clue why.

    2. @EventHandler
    3. public void onBottleDrink(PlayerItemConsumeEvent e) {
    4. if (e.getItem().getType() == Material.POTION) {
    5. for (int i = 1; i < e.getPlayer().getInventory().getSize(); i++) {
    6. if (e.getPlayer().getInventory().getItem(i).getType() != null) { // THIS LINE. NO IDEA WHY.
    7. if (e.getPlayer().getInventory().getItem(i).getType() == Material.GLASS_BOTTLE) {
    8. e.getPlayer().getInventory().setItem(i, new ItemStack(Material.AIR));
    9. break;
    10. }
    11. }
    12. }
    13. }
    14. }

    thanks in advance.
  2. Online

    timtower Moderator Moderator

  3. Offline


    mhm, legit just realised that, thank you.

    EDIT: Last thing, I'm sure this should work:

    2. if (e.getPlayer().getInventory().getItem(i) == new ItemStack(Material.GLASS_BOTTLE))

    this is never true (I'm making a plugin that removes glass bottles when you drink them, is it because it's too fast?
  4. Online

    timtower Moderator Moderator

  5. Offline


    was also thinking, just didn't do it >.<
    thanks again;

    EDIT: found out it's too fast, used a bukkitrunnable to fix, but just as a note for anybody here ;P
    Last edited: Jul 9, 2018
  6. Offline


    @MemeplexOwner if you think about it it's kinda logic. If an event should be cancellable in any way the plugin should be notified about it before it really happens. Only that way it can be stopped before happening.
Thread Status:
Not open for further replies.

Share This Page