Player Interact while holding a certain item, null pointer issue?

Discussion in 'Plugin Development' started by h4344, Dec 18, 2011.

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

    h4344

    Im getting a severe error pop up in the console for this line of code. I tried adding a check for if the material was null but it ddnt seem to work. I also tryed adding else if material != flint_and_steel but it still didnt work.

    What i tried.
    Code:
    if (event.getItem().getType() == Material.FLINT_AND_STEEL && event.getItem().getType() != null) {
     
  2. Offline

    Trc202

    Item itself is an object in java and can be null (such as when the player has nothing in their hand). What you should try is:
    Code:
    if ((event.getItem()  !=  Null) &&  (event.getItem().getType() == Material.FLINT_AND_STEEL){
    //do something
    }
    
    This checks to see if the object is null and skips the event if it is. (Note: Order matters here. Java checks from the left to the right)
     
  3. Offline

    Perdog

    I'm just curious as to why you need a null check? If the item in their hand isn't flint and steel it wouldn't pass through, so I don't understand the null check
     
  4. Offline

    h4344

    Thank you :D it works perfectly.
     
  5. Offline

    wwsean08

    because if there is no item in their hand it event.getItem() will be null, so when they go to do event.getItem().getType() it will throw a null pointer exception
     
  6. Offline

    Perdog

    Oh crap ... Never thought about that .. I always just thought it would return it as air or something ... I think I should go do some null checking in a few of my plugins :p
     
  7. Offline

    wwsean08

    air is considered null in the inventory, i went through that same situation working on ClearInv back in the beginning
     
Thread Status:
Not open for further replies.

Share This Page