简体   繁体   中英

Exception is thrown, Page is called, but object is null

I am new to SpringMVC and I am running into a strange error. On purpose I've created a "NullPointerException" to test my ErrorPage handling and Exception handling.

Now I have this Method which requests the mapping for the NullPointerException

@ExceptionHandler(NullPointerException.class)
public ModelAndView handleCustomException(NullPointerException ex) {

    ModelAndView model = new ModelAndView("ErrorPage");
    model.addObject("status", "NullPointerException: " + ex.getMessage());

    return model;

}

The method is called, I know this because I can see the actual page, but for ex.getMessage() there is always "null" (and the object is null).

Can someone explain to me why and how I can solve this?

For completeness:

The exception was thrown but as @Tunaki said, an exception thrown with

Object a = null; a.doSomething(); 

has no message. So I created a new NullPointerException with a message and throw it manually. Problem solved!

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