March 7, 2013

Checking Array for null element

Question by V1rtua1An0ma1y

So I’ve created my own class with 5 private fields: Each is an array with a pre-set length. It’s my way of creating a table, where each array is a column, and they have pre-set lengths because not every cell will contain an element (So, not using anything dynamic).

Anyway, my question is: Can I check to see if a specific cell of a specific array contains “null”? Using .equals(null) gives a nullpointerexception 🙁

Answer by user000001

When you call .equals(...) you call a method of the object. If it is null, it has no method. Therefore null check like this:

if (myArray[position] == null) {
    ....

}

Answer by Starx

Mixed up for loops and null construct

for(Integer ints : intNum) {
    if(intNum != null) {
      //valid
    }
} 

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!