Reputation: 211
I have the following code:
RoomCell r = b.GetRoomCellAt(1, 2);
assertTrue(r.isDoorway());
The second line is failing with a null pointer exception, as if r hadn't been instantiated. But I'm setting it equal to a cell that that method retrieves, so it is obviously not null.
Why would I possibly be getting this error?
Upvotes: 0
Views: 124
Reputation: 106508
The only way you'd be getting a NullPointerException
is if r
was indeed null
. You can put this in your test conditions to ensure that, if r
actually is null
, then the test will fail:
RoomCell r = b.GetRoomCellAt(1, 2);
assertNotNull(r);
assertTrue(r.isDoorway());
If r
is null
, then the test fails early, and you avoid the NPE.
Upvotes: 0
Reputation: 8466
if(r != null)
{
assertTrue(r.isDoorway());
}
instead of
assertTrue(r.isDoorway());
b.GetRoomCellAt(1, 2);
This cell may be null. So only you are getting nullPointerException
Upvotes: 0
Reputation: 89839
What's your full stacktrace? It is needed in order to determine which of the following is the cause:
It's possible that GetRoomCellAt doesn't find a value and returns null
It's possible that the implementation of GetRoomCellAt accesses a null pointer
It's possible that the implementation of isDoorway accesses a null pointer
FYI, it is good practice to use the @Nullable annotation to annotate parameters and return types
Upvotes: 3