Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception.getMessage() is null

Tags:

java

exception

Inside my Java code, it is checking for !null condition and throwing an Exception.

For example

try {     if (stud.getCall() != null)         acc.Call = stud.getCall().toString();     else         throw new Exception("Data is null"); } catch (Exception e) {     logger.error("Some Error" + e.getMessage());     throw new Exception("Please check the Manatadatory Field is Missing" + e.getMessage()); } 

But in the logs I am getting:

Some Error null 

Why is the e.getMessage null?

like image 721
Revathi Avatar asked Nov 23 '11 03:11

Revathi


People also ask

Why is e getMessage null?

You are catching a different exception to the one that your code is explicitly creating and throwing1. The exception that you are catching doesn't have a message. You need to log the entire exception, not just the exception's message.

What is exception getMessage ()?

The getMessage() method of Throwable class is used to return a detailed message of the Throwable object which can also be null. One can use this method to get the detail message of exception as a string value. Syntax: public String getMessage()

Why is my exception null?

NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.

What is the use of getMessage ()?

The GetMessage function retrieves messages associated with the window identified by the hWnd parameter or any of its children, as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters.


1 Answers

You are catching a different exception to the one that your code is explicitly creating and throwing1. The exception that you are catching doesn't have a message.

You need to log the entire exception, not just the exception's message. Among other things, that would tell you what the caught exception's actual class is and where the exception was created/thrown.

Based on the fact that the exception doesn't have a message, I'd guess that it is an NPE caused by stud or acc being null, or by stud.getCall() returning null ... or something like that. A NullPointerException generated natively (i.e. by the JVM) has a null message2.


Throwing java.lang.Exception is Bad Practice

Your problem illustrates why it is generally speaking a bad idea to create/throw Exception

When you throw Exception it becomes next to impossible to discriminate between it and other (unexpected) exceptions in a catch clause. And that is what has happened here: you've caught the wrong exception.

You should pick a more specific exception, and if no appropriate one exists, implement one of your own.


1 - You could use e.printStackTrace(), a logger call, or a debugger to see which exception you have actually caught.
2 - This is not true on Android. There, an NPE has a helpful message that gives contextual information. It is also no longer true in Java 14 and later; see https://openjdk.java.net/projects/jdk/14/ and https://openjdk.java.net/jeps/358

like image 167
Stephen C Avatar answered Oct 25 '22 06:10

Stephen C