0

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?

3
  • 2
    The second approach correctly deals with the situation that you avoid the NPE for element. However, what you will have to consider further, is that a might be null, if element was null. Commented Sep 13, 2021 at 8:58
  • nulls have a tendency of breaking your code, because you cannot use it as an object at all. You may find it beneficial to code to a style where you avoid using nulls if at all possible. Java have had added libraries to help you, but it does not enforce it. Look at Optional if you need a wrapper. Commented Sep 13, 2021 at 9:11
  • 1
    One remark on the code shown (not directly related to your question): the System.out.println("..."); might be meant as an error message if element is null. Throwing an exception might be a better choice. Commented Sep 13, 2021 at 9:51

3 Answers 3

1

I got a null pointer exception on this code

Yes, because you don't not execute element.getText() if element == null.

Element element = ...
if(element == null) {           // This check is irrelevant to...
   System.out.println("...");
}
a = element.getText();          // ...whether this statement is executed.

Is it a good solution

It's a solution, because the else isn't executed when element == null.

You might consider inverting the condition, so the "happy" case (the one you want to execute when things are working normally) comes first. But this is not an important difference, functionally.

if(element != null) {
   a = element.getText();
} else {
   System.out.println("...");
}
Sign up to request clarification or add additional context in comments.

Comments

1

You approach is ok since it works and you dont get the NPE anymore.

You could invert the if condition and simplify things like:

String a= null
Element element = ...
if(element != null) {
   a = element.getText();
} 

This way you do your things in a protected code block and you don't need else part unless you really wanted to print "..."

Comments

0

Yes it is a solution for the reasons already mentioned by the other answers. I want to provide another solution:

String a;
try {
    Element element = ...;
    a = element.getText();
} catch (NullPointerException e) {
    System.out.println("...");
    a = null; // Or some other value that can be tested but cannot produce a NPE
}

This would have the same error handling as your if statement, but it has the advantage, that ´element´ cannot be referenced later and produce a NPE somewhere else. This is of course only useful if the String cannot produce a NPE either way and if the Element could actually used later (in other words if the method doesn't end directly afterwards).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.