My Object Is Not Null But I'm Getting A Null Pointer Exception
Solution 1:
In the stacktrace I see this:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
The adapter is trying to call size()
on a null List
. Make sure your list searchResults
is not null.
Solution 2:
NullPointerException
occurred because searchResults
is not initialized and is null
.
Regarding your question in the comment -
do you know why the error did not actually point to the line where searchResults was?
ArrayAdapter
calls getCount()
internally whenever you call setAdapter()
or refresh the already created adapter using notifyDataSetChanged()
.
Till you call setAdapter()
or notifyDataSetChanged()
, ArrayAdapter
won't create view for you and in order to create that, it needs to check the size of the data which you passed to it.
For that it calls getCount()
which is
publicintgetCount() {
return mObjects.size();
}
where mObjects
is initialized to the data list which you pass while creating your adapter.
Apparently mObjects.size()
will throw NullPointerException
since mObjects
is null
because searchResults
is null.
Hope you understood.
Post a Comment for "My Object Is Not Null But I'm Getting A Null Pointer Exception"