The cause value is the cause of this throwable exception. To make getCause() return a value you can check an example of my peace of code.
class Dog {
public void makeSound() {
System.out.println("Bark Bark");
throw new NullPointerException("message");
}
}
try {
// create an object of Dog
Dog dog = new Dog();
// create an object of Class
// using getClass()
Class obj = dog.getClass();
// using object of Class to
// get all the declared methods of Dog
Method[] methods = obj.getDeclaredMethods();
// create an object of the Method class
for (Method m : methods) {
Object[] parameters = new Object[] {};
m.invoke(dog, parameters);
}
} catch (InvocationTargetException e) {
System.out.println(e);
} catch (Exception ex) {
System.out.println(ex);
}
FYI:
e.getTargetException()
is the same as e.getCause()
InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.
InvocationTargetException DOC link
Some related info Chained Exceptions
Throwable
; 2..getCause()
tries and sees what the parentThrowable
is, if any. – Iniquitoust
variable is theNoClassDefFoundError
. Just callSystem.out.println(t)
orSystem.out.println(t.getMessage())
. If you expandt
in Eclipse, you'll find that that exception doesn't have a cause... I think you've misunderstood whatgetCause()
is there for. – Pumice