Java – why do you get a NullPointerException when comparing a string with null?

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
分享
二维码
< <上一篇
下一篇>>