Why do I get a NullPointerException when comparing a String with null?

前端 未结 4 1968
别那么骄傲
别那么骄傲 2020-12-06 05:20

My code is breaking on the following line with a nullpointerexception:

 if (stringVariable.equals(null)){

Previous to this statement, I dec

4条回答
  •  余生分开走
    2020-12-06 06:01

    Use

    stringVariable == null
    

    To test whether stringVariable is null.

    The equals method (and every other method) requires stringVariable to not be null.

提交回复
热议问题