简体   繁体   中英

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

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().

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.

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) {

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, is it has something to do with concurrent access to the same object hForm, some method may be assigning "null" for hForm or id.是它与对同一对象 hForm 的并发访问有关,某些方法可能分配“null”对于 hForm 或 id。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM