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) {

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