I have a line of code that when I am debugging a nullpointerexception I can see that it is the cause of my app crashing.
The line of code that is causing the nullpointer is this:
ListView storeList = (ListView) findViewById(R.id.storeList);
and then in debug mode it crash when it tries to assign this:
storeList.setAdapter(arrayAdapter2);
I do not know why the object is not initializing. The ArrayAdapter below it initializes fine. If you
Below is the actual java activity file:
public class StoreListView extends Activity {
UserFunctions userFunctions = new UserFunctions();
ArrayAdapter<String> arrayAdapter2;
ArrayList<String> spinnerArray;
protected void onCreate(Bundle savedInstanceState) {
ListView storeList = (ListView) findViewById(R.id.storeList);
super.onCreate(savedInstanceState);
setContentView(R.layout.storelistviewpage);
arrayAdapter2 = new ArrayAdapter<String>(StoreListView.this,android.R.layout.simple_list_item_1);
spinnerArray = getIntent().getStringArrayListExtra("cusName");
arrayAdapter2.addAll(spinnerArray);
storeList.setAdapter(arrayAdapter2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_screen, menu);
return true;
}
}
And below here is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/storeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical" >
</ListView>
Edit:
In case some are wondering I added the activity to the android mainfest
Move
after the
setContentView()call.Since your current code assigns the result of
findViewById()beforesetContentView()is called, you will always getstoreListbeingnull, and so, you’ll get an NPE.