Nullpointer exception

If you use

if (x == null)

you will not get a NullPointerException.

I suspect you're doing:

if (x.y == null)

which is throwing because x is null, not because x.y is null.

If that doesn't explain it, please post the code you're using to test for nullity.


I guess you are doing something like this,

  String s = null;

  if (s.equals(null))

You either check for null like this

  if (s == null)

A better approach is to ignore the null and just check for the expected value like this,

  if ("Expected value".equals(s))

In this case, the result is always false when s is null.


String is immutable

@Test(expected = NullPointerException.class)
public void testStringEqualsNull() {
    String s = null;
    s.equals(null);
}

@Test
public void testStringEqualsNull2() {
    String s = null;
    TestCase.assertTrue(s == null);
}