1

I currently get the NullPointerException in my following code.

else if (isEmpty(href))
    System.err.println("This is an empty link: " + href);

Where the isEmpty function is the following.

public boolean isEmpty(String HREF){
    if (HREF.equals("") || HREF.equals("null") || HREF == null || HREF.isEmpty())  
        return true;
    else 
        return false;
}

Is it because I can not compare a string to null? What can I do to make this function work?

3
  • 1
    try HREF=="null" and also it should be the first condition being checked Commented Jul 23, 2015 at 15:18
  • 3
    Look up java boolean operator short circuit, and then see why we recommend that the null check be done first. Note that @digidude is incorrect -- you do not surround null with quotes. It's if (HREF == null || ...) Commented Jul 23, 2015 at 15:20
  • Also when you do the comparison HREF.equals("null") , you are comparing HREF to a string "null", which could result in incorrect output when HREF="null" Commented Jul 23, 2015 at 15:22

5 Answers 5

12

Your isEmpty test is just doing things in the wrong order. Change this:

if (HREF.equals("") || HREF.equals("null") || HREF == null || HREF.isEmpty())  
    return true;

to:

if (HREF == null || HREF.equals("") || HREF.equals("null") || HREF.isEmpty())  
    return true;

If HREF is actually null, then the first test will short-circuit the rest of the if and you won't get a NPE.

(By the way, explicitly testing HREF.equals("") and also calling HREF.isEmpty() is redundant. You only need to do one or the other.)

By the by-the-way, I would recommend, as a matter of style simplifying your method to a single return statement:

public boolean isEmpty(String HREF){
    return HREF == null || HREF.isEmpty()) || HREF.equals("null");
}

(Also, if testing against the string "null" was an attempt to check against a null value, you can drop that condition as well. In fact, you could then use something like Apache Commons' StringUtils.isEmpty(CharSequence) method to do what you want.)

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

5 Comments

If he just changes the order in HREF.equals("") then there will be no need to check for HREF == null || HREF.isEmpty()
@Ted .... keeping the check HREF.equals("null") could result in incorrect behavior when HREF="null" as he is doing a string comparison there.
@DanilGaponov - I agree about the HREF.isEmpty(), but "".equals(HREF) will be false if HREF is null, so an explicit null test would still be needed.
@digidude - I assumed that OP actually wants to treat the string "null" as "empty". Without knowing the broader logic of the program, I saw no need to change OP's semantics of the method.
@TedHopp ... Yeah. I just assumed otherwise. Let's leave it for him to decide .... I thought I would just put it out there. :)
1

changing the order in your if will solve it.

public boolean isEmpty(String HREF) {
    return HREF == null || HREF.equals("") || HREF.equals("null") ||  HREF.isEmpty();
}

this way, when HREF == null, none of the other tests will be evaluated.

Comments

1

When HREF is null , referencing it with dot operator will throw a null pointer exception .

Different ways to solve this is as follows

  public boolean isEmpty(String HREF){
      if (HREF == null ||HREF.equals("") || HREF.equals("null") ||    HREF.isEmpty())  
          return true;
      else 
          return false;
 }

this is by just re arranging your conditions, by short cut of boolean operations, this will avoid referencing a null pointer in subsequent checks.

OR

  public boolean isEmpty(String HREF){
      //handling null case first of all
      if(HREF == null)   return true;

      if(HREF.equals("") || HREF.equals("null") ||    HREF.isEmpty())  
          return true;
      else 
          return false;
 }

Also avoid redundant use of checks like HREF.equals("") and HREF.isEmpty() are similar.

Another best practice to check equals with a string object is to use the constant in the beginning as in "".equals(HREF) and "null".equals(HREF), here we are pretty sure that "null" is a valid string and which is not null

Comments

0

You cannot call methods of the null here HREF.equals("") Common practice is to change the order "".equals(HREF) . This is null safe. And you will not need HREF.isEmpty() part this way. So the code will become

public boolean isEmpty(String HREF){
     return HREF == null || "".equals(HREF) || "null".equals(HREF);
}

I have removed the unnecessary if here as we are returning the result of boolean expression.

Comments

-1

If HREF is null, then doing this:

HREF.equals("")

will throw a NPE.

Do the null check first:

HREF != null && /* rest of conditions */

1 Comment

@engineercoding: we all know that, I am just trying to have the answer itself be more complete so I can up-vote it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.