Hi, I'm back again! I have this code: Code (Java): for(int slot = 20; slot <= 24; slot++) { if(inv.getItem(slot) == null) { inv.setItem(22, item); } } I'm checking if slots 20,21,22,23,24 is empty and and if so I want to set the item.
Yeah, that's super old Your slots could be null or air, so you have to check both. Also, in your code change 22 to slot.
I have this: Code (Java): if(inv.getItem(slot) == null || inv.getItem(slot).getType() == Material.AIR || inv.getItem(slot).getType() == null) { But this setItem everytime.
Are you sure there are items in those slots? Maybe you have the slot numbers wrong. http://wiki.vg/Inventory
Slots are right. I remove it: Code (Java): if(inv.getItem(slot) == null || inv.getItem(slot).getType() == Material.AIR) { But this setItem everytime.
Code (Java): for (int slot = 20; slot <= 24; slot++) { if (inv.getItem(slot) == null || inv.getItem(slot).getType() == Material.AIR ) { inv.setItem(slot, item); } } This will put the item where there are no item
But I need to set item on 22. I want check if slots 20,21,22,23 and 24 are null/air and if so I want to set the item on 22.
Can you show us the full code with full error message? In my opinion the inventory is null. null.getItem() -> I think this is why you get the error. Show me how do you get/create inventory?
If inventory is null it's normal that it throw nullpointer. You have to check for null inventory and after check with only one if for the slots that you want to check
In your code you set the item every time ONE of the slots is null not if all of them are EDIT: you could use: Code (Text): boolean b = true; for (int slot = 20; slot <= 24; slot++) { if (inv.getItem(slot) != null && inv.getItem(slot).getType() != Material.AIR ) { b=false; } } if(b) { setItem(22,item); }