Solved Menu get null slots.

Discussion in 'Spigot Plugin Development' started by SmaxoS, Jun 21, 2018.

Thread Status:
Not open for further replies.
  1. Hi, I'm back again! :ROFLMAO:

    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.
     
  2. Slots are never null anymore. Check for Material.AIR instead
     
    • Informative Informative x 1
  3. This give me a error:
    Code (Text):
    java.lang.NullPointerException
     
  4. What version are you using?
     
  5. Spigot 1.8.8
     
  6. 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.
     
  7. 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.
     
  8. Are you sure there are items in those slots? Maybe you have the slot numbers wrong.

    http://wiki.vg/Inventory
     
  9. FrostedSnowman

    Resource Staff

    getType() will never return null
     
    • Informative Informative x 2
  10. Slots are right.
    I remove it:
    Code (Java):
    if(inv.getItem(slot) == null || inv.getItem(slot).getType() == Material.AIR) {
    But this setItem everytime.
     
  11. 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
     
  12. 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.
     
  13. 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?
     
  14. 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
     
  15. 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);
    }
     
    • Winner Winner x 1
Thread Status:
Not open for further replies.