Skip to content
Advertisement

IntelliJ warns NullPointerException

I have a method that can return a String and can be a null value, using this code:

JavaScript

However, commenting on the exit, I no longer receive this warning

JavaScript

Why does it happen?

Advertisement

Answer

System.out.println(content == null); this line indicates that content could possibly become a null, so which would possibly cause a NullPointerException in content.length(). So the IDE warns about a potential NullPointerException.

So once you remove the null check, then the IDE doesn’t see a scenario where content would be null (Even though it could still be null but there is no explicit null check). Hence the warning will not be shown in this case.

So you could do:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement