NullPointerException with boolean check

Discussion in 'Plugin Development' started by Colby l, Feb 26, 2014.

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

    Colby l

    The code below is suppose to check if an item has an item meta, and return false if it doesn't. However, when I run the loop, I receive a NullPointerException at
    Code:java
    1. if (is.hasItemMeta())


    Full Code:

    Code:java
    1. for (ItemStack is : inventory) {
    2.  
    3. // TEST
    4. if (is != null && !is.equals(Material.AIR)) {
    5.  
    6. continue;
    7. }
    8.  
    9. if (is.hasItemMeta()){
    10.  
    11.  
    12. continue;
    13. }
    14.  
    15. }


    Thanks for the help!
     
  2. Colby l
    That's because if the player has an empty slot it'll return null.

    So add this just after the for loop.

    Code:
    if(is == null){
        continue;
    }
     
  3. Offline

    Rocoty

    Also. Not related to your current problem, but to one you are likely to get in the future. The ItemStack will never be equal to a Material. Use the item stack's type to compare.
     
Thread Status:
Not open for further replies.

Share This Page