[Solved] Java: How can I tell if one of my array String values is null?


How can I tell if one of my values is/are null?

One way is to learn to read the stack trace that you are getting. (We could maybe help you with that, if you showed it to us.)

Another way is to test the value to see if it is null.

Is there a test?

Here is how you test a simple variable.

    if (a == null) {
         System.out.println("a is null");
    }

If you have an array whose elements could be null, write a loop to test them. (There are more elegant ways … but if you are learning, keep it simple.)

How did it get to be null?

The two ways are:

  • You assigned null to it.
  • It was null to start with, and you haven’t assigned a non-null value to it. For example, new String[5]; creates a new String array whose values are initially all null.

And most importantly, is this even the correct question to be asking?

We can’t tell you what the “correct” question is, because it depends on your mental state … and we cannot read your mind.

0

solved Java: How can I tell if one of my array String values is null?