I have the following code:
String a= null
Element element = ...
if(element == null) {
System.out.println("...");
}
a = element.getText();
I got a null pointer exception on this code. I thought that it would be better to use an if else statement in order to avoid this error.
String a= null
Element element = ...
if(element == null) {
System.out.println("...");
} else {
a = element.getText();
}
Is it a good solution to use the above code to solve the problem or it would be better to manage it another way?
element. However, what you will have to consider further, is thatamight benull, ifelementwasnull.System.out.println("...");might be meant as an error message ifelementisnull. Throwing an exception might be a better choice.