user8670299
user8670299

Reputation:

Comparing Two Strings give java.lang.NullPointerException

I'm trying to get checkbox value from HTML post. My problem is that the post return on or null instead of true or false so I need to convert it to a true of false to insert in the database.

 Boolean hasProfile7 = Boolean.valueOf(request.getParameter("hasProfile7"))

hasProfile7 is false even when it's "on"

 Boolean hasProfile7 = (request.getParameter("hasProfile7").equals("on")) ? true : false;

This crashes my application giving java.lang.NullPointerException when the checkbox is not ticked (false)

 Boolean hasProfile7 = (request.getParameter("hasProfile7") == "on") ? true : false;

and this gives me always false.

What can I do?? I just want the bool to be True when "on" and false when "null"

Upvotes: 0

Views: 874

Answers (2)

zappee
zappee

Reputation: 22716

This is the null safe way to compare constant with String:

"some-value".equals(variable) // ok

The following code snippet will throw a null pointer exception if variable is null because you are not allowed to call equals method on null value:

variable.equals("some-value") // not recomended

This method will return true if value contains "on", otherwise false will be returned:

public boolean isChecked(final String value) {
    if (Objects.isNull(value)) {
        return false;
    }

    if ("on".equals(value.toLowerCase())) {
        return true;
    }

    return false;
}

Upvotes: 3

Erwan C.
Erwan C.

Reputation: 719

First point

Boolean hasProfile7 = Boolean.valueOf(request.getParameter("hasProfile7"))

You are doing

Boolean.valueOf(<A String>)

According to https://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html#valueOf(java.lang.String) public static Boolean valueOf(String s) "Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true"."

Second point

Boolean hasProfile7 = (request.getParameter("hasProfile7").equals("on")) ? true : false;

Here you are close to the rigth thing to do. Your problem is that request.getParameter("hasProfile7") returns null and so you have *null*.equals("on")and thats why you get the NPE.

When you have a predefined String, always put it on the left size:

Boolean hasProfile7 = ("on".equals(request.getParameter("hasProfile7"))) ? true : false;

Here, even if request.getParameter("hasProfile7") is null you have any exception and it will simply return false

Third point

 Boolean hasProfile7 = (request.getParameter("hasProfile7") == "on") ? true : false;

Here you are comparing the object "on" to the object request.getParameter("hasProfile7"). If the values are the sames, the objects are not.

For the last point, why do you use a ternary expression (request.getParameter("hasProfile7").equals("on")) ? true : false; can be translated to

if true --> true 
if false --> false

You can just write

Boolean hasProfile7 = "on".equals(request.getParameter("hasProfile7"))

Upvotes: 1

Related Questions