0

An array of Strings, names, has been declared and initialized. Write the statements needed to determine whether any of the the array elements are null or refer to the empty String. Set the variable hasEmpty to true if any elements are null or empty-- otherwise set it to false.

   hasEmpty=false;
   for (int i=0;i<names.length;i++)
   if (names[i].trim().equals("") || names[i]==null)
   hasEmpty=true;

Whats wrong with my code?

7
  • @ColinD what was edited? Commented Oct 17, 2010 at 2:24
  • 1
    I added the homework tag, as this reads like a homework assignment. If it's not, my mistake. But looking at your question history, I think you need to spend more time learning this yourself and a little less asking questions for every thing. If you take the time to figure some of these things out yourself, you'll probably end up understanding it better. Commented Oct 17, 2010 at 2:31
  • I only ask if I spend too much time on it..most of the answers I received were stuff that I would have never figured out on my own anyway..like checking for null before using trim. I would have never figured that out, even if I had spent hours on it.. Commented Oct 17, 2010 at 2:34
  • I think you could have figured it out. Commented Oct 17, 2010 at 2:40
  • Actually this code was failing even without trim(), so even if I had names[i].equals(""), it was failing, so I probably would not have figured it out.. Commented Oct 17, 2010 at 2:43

4 Answers 4

2

Calling trim() first will result in a NullPointerException should a member of the array be null. Reverse the order of the conditions - the short-circuiting nature of || will then ensure that trim is only called on a real String object.

Sign up to request clarification or add additional context in comments.

Comments

0

Consider names[i].trim().

When names[i] is a String, you really have something like someString.trim() which works fine.

When names[i] is a null, however, you really have something like null.trim(). You've already discovered that null doesn't allow a trim() method. (In fact, I'm not even really sure what 'null' is.)

Therefore, you must check for null before you invoke trim().

When you have a && b, where a and b are expressions, the checks are made left-to-right and the parser stops as soon as the issue is settled. So for the logical and operator (&&), if a is false then b is never checked. This is what allows

if (a != null && a.trim().length() > 0) { ... }

to work. if a is null, the a.trim() part is not executed since it would be pointless from a logical point of view; the value of the conditional has been decided.

Similarly for

if (a == null || a.trim().length() == 0) { ... }

if a is null then the a.trim() part is never performed and we don't get an error.

2 Comments

" In fact, I'm not even really sure what 'null' is." - See java.sun.com/docs/books/jls/third_edition/html/…
hehe I looked it up immediately upon posting. It was pretty much what I thought but reading it is good.
0

You can use the Apache Commons Lang's isBlank() to check a String:

if (StringUtils.isBlank(names[i]) {
    ...
}

StringUtils.isBlank is checking if the String is null or empty (i.e. if it is equals to "" when all blank characters are removed).

Comments

0

It's throwing a Null Pointer Exception because you're trying to run a method on a null object:

if (names[i].trim().equals("") || names[i]==null)

So, anytime that names[] has ONE name that's null, it will throw the exception. One way to solve the problem is to switch the boolean statements in this if statement:

if (names[i]==null || names[i].trim().equals(""))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.