-3

i'm getting the error Nullpointerexception in my java codes. Here is my stacktrace:

     --------- beginning of crash
05-22 19:59:56.285 3050-3050/com.example.it3783.listview E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.listview, PID: 3050
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                               at com.example..listview.MainActivity$1.onItemClick(MainActivity.java:50)
                                                                               at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                                                                               at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
                                                                               at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
                                                                               at android.widget.AbsListView$3.run(AbsListView.java:3879)
                                                                               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)

Here are my java codes:

   public class MainActivity extends AppCompatActivity {

    ArrayAdapter<CharSequence>adapter; //define adapter
    TextView tvSelection; //define the superhero display text
    ListView lvLocation; //define the location in listview

    String []strArray; // string array for the 4 locations
    String []superHeros = {"1. Batman\n 2. SpiderMan", "1. Peter Pan\n2. Superman",
            "1. IronMan\n2. Peter Pan", "1. SnowWhite \n2. Gingerbreadman"};
    //string for the superhero names


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adapter = ArrayAdapter.createFromResource(this, R.array.locationArea, android.R.layout.simple_list_item_1);

        Resources myRes = this.getResources();
        strArray = myRes.getStringArray(R.array.locationArea);

        //create an adapter linked to the objects of the place
        adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, strArray);

        //superheroes to find the superhero linked to location
        tvSelection = (TextView) findViewById(R.id.superHerosTV);
        lvLocation = (ListView) findViewById(R.id.spLocations);
        //locations to click on location

        //Link the listview to the objects of the location
        lvLocation.setAdapter(adapter);



        lvLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String text = superHeros[position];
                tvSelection.setText(text);
            }
        });


    }
}

Here are my xml codes:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.listview.MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Choose a location..."/>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:id="@+id/spLocations"></ListView>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@+id/superHerosTV"
        />

</LinearLayout>

And my strings.xml:

 <resources>
    <string name="app_name">ListView</string>

    <string-array name="locationArea">

        <item>North</item>
        <item>South</item>
        <item>East</item>
        <item>West</item>

    </string-array>

</resources>

Apparently the error comes from the last line of my java code "tvSelection.setText(text);". Please help I'm confused as to what's wrong with my codes.

Mona
  • 15
  • 4

2 Answers2

2

In your XML code

android:text="@+id/superHerosTV"

should be

android:id="@+id/superHerosTV"

Keep in mind that findViewById(...) will return null if there's no view with the given id. That's because your code doesn't work.

Flood2d
  • 1,338
  • 8
  • 9
1

The problem is this line :

android:text="@+id/superHerosTV"

It should be :

android:id="@+id/superHerosTV"

tvSelection = (TextView) findViewById(R.id.superHerosTV) will be always null because the view can't be found