1.8.8 Maybe really stupid NullPointerException...

Discussion in 'Spigot Plugin Development' started by zt38475tz, Feb 3, 2020.

  1. Hello guys,

    Code (Text):
    Player player = (Player) sender;
            ItemStack barrier = new ItemStack(Material.BARRIER);

            for (int i = 0; i <= 8; i++) {
                if (player.getInventory().getItem(i).getType() != Material.AIR && player.getInventory().getItem(i).getType() != null) {
                    player.getInventory().setItem(i, barrier);
                }

            }
    I am getting a NullPointerException at the line where the if statement is getting executed. I really don't know what is going on.
     
  2. getItem(i) is null
     
  3. @Plochem and how I am supposed to fix it? I don't really understand that right now
     
  4. check if getItem() is not null.
     
  5. @Plochem I want to check if the hotbar of the player is empty and then set set the items in the hotbar, that didn't work
     
  6. then check if it's null
     
  7. Yeah I know, I forgot to remove the second if statement. Thanks anyways!