3

I'm trying to display data in a listView. I have the current code attempting to take an array that is passed in from the myDatabase class, and then display this within my listView.

However I got an error:

"Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference".

My code is working in main activity when I remove the code for the listView element, in it's place I've tested to ensure the passed in array outputs correctly (which it does). I'm not sure what I'm doing wrong to have this error thrown up.

MainScreenActivity.java

public class MainScreenActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    // Find View-elements
    Button trackButton = (Button) findViewById(R.id.trackSelectButton);

    // Create click listener for trackSelectionButton
    trackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {                
            trackInput();

            // Start Track List Activity
            Intent trackScreen = new Intent(v.getContext(), TrackListActivity.class);
            startActivity(trackScreen);
        }
    });        
}

public void trackInput(){

    MyDatabase mDb = new MyDatabase(this);

    String[] tracks = mDb.fetchDatabaseTracks();
    ArrayAdapter<String> trackAdapter =
            new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    android.R.id.text1, tracks);

    ListView lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(trackAdapter);
}

TrackListActivity.java

public class TrackListActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_track_list);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

}

activity_track_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.autoplaylist.catalog.TrackListActivity">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

activity_main_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainScreenActivity">

<TextView
    android:id="@+id/welcomeMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/opening_message"
    android:textSize="18sp"
    android:fontFamily="sans-serif-light"
    android:layout_marginBottom="10dp"/>

<RelativeLayout
    android:id="@+id/relLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/trackSelectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Track selection" />

</RelativeLayout>

</LinearLayout>

Error log

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.autoplaylist.catalog.MainScreenActivity.trackInput(MainScreenActivity.java:61)
at com.example.autoplaylist.catalog.MainScreenActivity$1.onClick(MainScreenActivity.java:29)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Luke Beveridge
  • 505
  • 1
  • 6
  • 19
  • 1
    Your ListView is not in the main layout (activity_main_screen) therefore using just ***findViewById(R.id.listView)*** won't work. Have you tried putting the listview in the main layout? Or is it supposed to be in a different activity that is using the ***activity_track_list.xml*** ? – AL. Mar 09 '16 at 03:41
  • I want to display my listView within **activity_track_list,xml**, I'm not entirely sure what is required to do this. Would I need to do the work to display the listview within the my other activity? (I'll update my question with that class). Although, for testing sake I've tried out @Jois answer below, putting the listView into my main layout - however another error appears: _java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference_ – Luke Beveridge Mar 09 '16 at 03:57

2 Answers2

12

You are trying to find the ListView element in your activity_main_screen xml where there is no such element. So, either add the ListView to your activity_main_screen or use the proper xml file.

Use

setContentView(R.layout.activity_track_list);

instead of

setContentView(R.layout.activity_main_screen);

and move

ListView lv = (ListView) findViewById(R.id.listView);

to onCreate()

Jas
  • 3,207
  • 2
  • 15
  • 45
  • I'm trying to achieve the second option of those two - so I would need to use **activity_track_list**. What I don't understand is how to reference this - apologies for what may seem to be a simple question but I am very new to this. – Luke Beveridge Mar 09 '16 at 04:15
  • See the updated answer – Jas Mar 09 '16 at 04:22
  • Thank you! I've spent hours looking at this... it was down to me using the wrong activity - I had confusion around when and how to use differing activities, thanks once more for clearing this up for me. – Luke Beveridge Mar 09 '16 at 04:35
  • Select my answer as accepted answer too – Jas Mar 09 '16 at 04:38
0

You dont have a ListView in mainScreen.xml. yOu are actually wiring up your activity to mainscreen.xml. and trying to display your List in tracklist.xml which is not possible. So if you want to display your list then do this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainScreenActivity">

<TextView
    android:id="@+id/welcomeMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/opening_message"
    android:textSize="18sp"
    android:fontFamily="sans-serif-light"
    android:layout_marginBottom="10dp"/>

<RelativeLayout
    android:id="@+id/relLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/trackSelectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Track selection" />

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

</LinearLayout>
Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42
  • Putting the listView into my layout **activity_main_screen.xml** causes for another error to appear: _java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference_ – Luke Beveridge Mar 09 '16 at 04:03