Java – why do you get a NullPointerException when comparing a string with null?
•
Java
My code uses NullPointerException to break in the following line:
if (stringVariable.equals(null)){
Before this statement, I declare stringvariable and set it as a database field@ H_ 301_ 5@
In this statement, I try to detect whether the field is null, but unfortunately it will break@ H_ 301_ 5@
Any ideas@ H_ 301_ 5@
Solution
use
stringVariable == null
Test whether the stringvariable is null@ H_ 301_ 5@
The equals method (and other methods) requires stringvariable not to be null@ H_ 301_ 5@
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码