I am a new to android ArrayAdapters and I am facing a null pointer exception when populating my ListView. The data which I used to populate the listview is not null, I had checked that. Also the whole thing is working fine on first load, but once I scroll down to the end of the list, the App crashes.
Here is my stack trace:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2386)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillGap(ListView.java:636)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5201)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4358)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:524)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
and Here's my Activity code: "I have marked the line where I am populating the listView"
public class AddPersonActivity extends Activity
implements
PlusClient.OnPeopleLoadedListener,
PlusClient.ConnectionCallbacks,
PlusClient.OnConnectionFailedListener {
private static final String TAG = "AddPersonActivity";
private static final String STATE_RESOLVING_ERROR = "resolving_error";
private ArrayAdapter sListAdapter;
private ListView sPersonListView;
private ArrayList<String> sListItems;
private PlusClient sPlusClient;
private boolean sResolvingError;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
Log.d(TAG, "onCreate called");
sPlusClient = new PlusClient.Builder(this, this, this)
.setActions(MomentUtil.ACTIONS)
.build();
sListItems = new ArrayList<String>();
sListAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1, sListItems);
sPersonListView = (ListView) findViewById(R.id.person_list);
sPersonListView.setAdapter(sListAdapter);
sResolvingError = savedInstanceState != null
&& savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart : connecting PlusClient");
sPlusClient.connect();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(STATE_RESOLVING_ERROR, sResolvingError);
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop : disconnecting PlusClient");
sPlusClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected : PlusClient connected");
sPlusClient.loadVisiblePeople(this, null);
}
@Override
public void onDisconnected() {
Log.d(TAG, "onDisconnected : PlusClient re-connect, PersonListView null");
sPlusClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed : connecting PlusClient");
sPlusClient.connect();
}
@Override
public void onPeopleLoaded(ConnectionResult connectionResult, PersonBuffer persons, String s) {
Log.d(TAG, "onPersonLoaded called");
switch (connectionResult.getErrorCode()) {
case ConnectionResult.SUCCESS:
sListItems.clear();
Log.d(TAG, "Loading People");
try {
int count = persons.getCount();
Log.d(TAG, String.valueOf(count));
for (int i = 0; i < count; i++) {
/*====== this is where listView is populated =====*/
sListItems.add(persons.get(i).getDisplayName());
}
} finally {
persons.close();
}
sListAdapter.notifyDataSetChanged();
break;
case ConnectionResult.SIGN_IN_REQUIRED:
sPlusClient.disconnect();
sPlusClient.connect();
break;
default:
Log.e(TAG, "Error when listing people: " + connectionResult);
break;
}
}
}
I tried logging at every stage to find the exact line which might be causing this, but there is nothing i could find. It'd be a lot of help if someone could let me know what am I doing wrong.
Thanks in advance!!!
I think one of the items in your ArrayList
is null. That's why it is giving NullPointerException
when you scroll down to that item. It would be better if you check if any item is null before adding it to ListView
.
int count = persons.getCount();
Log.d(TAG, String.valueOf(count));
for (int i = 0; i < count; i++) {
// this is where listView is populated
if(persons.get(i).getDisplayName() != null) {
sListItems.add(persons.get(i).getDisplayName());
}
}
Since you have mentioned that the list gets populated on the first load. It is only when the scrolling happens, the app crashes. One of the items in your persons list is null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With