pythonicate
pythonicate

Reputation: 172

Java exception when checking if Integer value is null

The following snippet of code causes my program the throw a null pointer exception and I'm struggling to determine why:

private void ...(){
    HierarchyForm hForm = (HierarchyForm)
    Integer id = hForm.getId();
    if (id != null && id.intValue() > 0){ <-- exception thrown here
        ...
    }
    .
    .
    .
}

When it crashes, the value of "id" is null. I know it's probably something simple but I can't understand why.

edit: here is a short program showing it failing. seems to be issue with .intValue comparison http://ideone.com/e.js/H0Mjaf

edit: i'm building for java 1.6.0_45

Upvotes: 0

Views: 11155

Answers (4)

paulohahn
paulohahn

Reputation: 1

You need to write like this:

private void ...(){
  HierarchyForm hForm = (HierarchyForm)
  Integer id = hForm.getId();
  if (id != null)
     if (id.intValue() > 0){ <-- exception thrown here
     ...
     }
  }
  . 
  .
  .
}

Okay, I had not contemplated that the "&&" in java had this behavior to solve the first expression and the second only solve if "true".

In this case, of course, I'm agree with the responses of colleagues and assuming you have posted the code correctly, my guess is it has something to do with concurrent access to the same object hForm, some method may be assigning "null" for hForm or id.

Upvotes: -4

darijan
darijan

Reputation: 9795

Only way that this lines causes an NPE is for id.intValue() to be executed on a null element.

Java would not execute id.intValue() if id != null is false, because the && is short-cutting the execution.

My suspicion is that your code actually looks like this:

if (id != null & id.intValue() > 0) {

whereas it should look like this:

if (id != null && id.intValue() > 0) {

Upvotes: 1

jmail
jmail

Reputation: 6132

Use This format to and find right solutions:

String id = request.getParameter("id");

        if(id!=null && !id.toString().equalsIgnoreCase(""))
        {
            user.setId(Integer.parseInt(id));
            dao.updateUser(user);
        }
        else
        {
            dao.addUser(user);
        }

If use that one otherwise type format:

String id = request.getParameter("id");

        if(id == null || id.isEmpty())
        {
            dao.addUser(user);
        }
        else
        {
            user.setId(Integer.parseInt(id));
            dao.updateUser(user);
        }

It is simple, put a null check! Surround your object with if statement like

Object mayBeNullObj = getTheObjectItMayReturnNull();

if (mayBeNullObj != null) 
   { 
     mayBeNullObj.workOnIt(); // to avoid NullPointerException
   }

But, All of them giving a same result.

Upvotes: -1

Vivek Vermani
Vivek Vermani

Reputation: 2014

The line shouldn't throw NPE if id is null.

If the first operand of && is false, the second operand isn't evaluated and the result is just false.

Please recheck your code again and make sure that your are getting NPE on this line while evaluating id.intValue().

Upvotes: 1

Related Questions