I can't catch the text message from a exception because ex.getMessage() is null.
. . .
Why is the ex.getMesage() null?
See the Java API for Exception.getMessage:
Returns the detail message string of this throwable.
- the detail message string of this
Throwableinstance (which may be
If you want help with code you have to post the code that you need help with.
The code fragment you posted is NOT what you need help with.
sometimes it would be nice to know what's null as it's not always instantly obvious looking at the code throwing the NPE
But yes, overall there's not much more to say.
I guess OP's confusion at least in part is caused by seeing "null" printed from the message of an NPE, probably expecting an empty String instead.
And don't tell me you don't sometimes make that mistake, printing out something and getting "null" in your output when you thought you'd get an "", we all do it, and then resort to things like System.out.println(x==null?"":x);
Actually I have made the mistake in the past of assuming the message is "null", as in a String containing null. And then I had fault handling code which assumed the message would never be null, and error out with a NullPointerException of its own. That's the most beautiful situation of all, where error handling routines produce exceptions of their own. Try to find the real truth then!