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

java, nullpointerexception

Solution

Use

stringVariable == null

To test whether `stringVariable` is `null`.

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

Problem

My code is breaking on the following line with a nullpointerexception: ``` if (stringVariable.equals(null)){ ``` Previous to this statement, I declare the stringVariable and set it to a database field. In this statement, I am trying to detect if the field had a `null` value, but unfortunately it breaks! Any thoughts?

Original source

Related problems