Several cases of error reporting in java.lang.nullpointerexception

Several cases of java.lang.nullpointerexception error reporting:

1. String variable is not initialized

2. Interface type objects are not initialized with specific classes, such as:

  List stuList ; In this case, a null pointer exception will be reported

  List stuList = new ArrayList(); After initialization, no error will be reported

3. When the value of an object is null and you do not judge it as null, a null pointer exception will also be reported

Therefore, add a judgment before the code, such as:

    if(ObjId != null);

For string type objects, you can make the following judgment:

    if(objectStr !== null && amp; !“”. equals(objectStr))

Of course, you can also judge whether it is not an empty string:

    if(objectStr !== null && amp; !“”. equals(objectStr.trim()))

 

Example: when I edit a page, it will pop up a custom error.jsp page code block (reporting system error)

Check the log: (an error is reported on line 54 — the picture is badly cut)

Because doc.getid does not make judgment, null pointer exception is reported when ID does not exist… (for case 3)

 

Similar Posts: