How to Solve Request processing failed; nested exception is java.lang.NullPointerException

1) Scene: use the request object under the HttpServletRequest package to perform the query request.getAttribute () and request.setAttribute () null pointer exception when assigning value
2) reason:
error example code:

because the value of provinceid is not assigned to the page before the jump, and then the value is assigned to the control layer here, but there is no provinceid object in the request object at all, so the tostring() of an empty object will definitely cause an error

3) Solution: however, we need to get the value in the form of string to prevent the occurrence of null pointer (that is, provinceid does not exist in the request object at all). We need to judge whether the request object is null

if(request.getAttribute("provinceId")!=null){
    //Execute the corresponding operation e.g. output
    String provinceId=request.getAttribute("provinceId").toString();
    System.out.println(provinceId)
}

Reminder: when the four common value taking methods in the controller operate, the received object must be judged to be null, and the object cannot be directly toString

Similar Posts: