-1

error is on setAdapter line. I can not find where is the error. Its is not about null exception. some thing is not in order

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.Namelist}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at

Main class:

public class Namelist extends AppCompatActivity {
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
        "WebOS","Ubuntu","Windows7","Max OS X"};
ListView l1;
ArrayAdapter<String> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    l1 = (ListView) findViewById(R.id.list);

    arrayAdapter = new ArrayAdapter<String>(Namelist.this,R.layout.activity_namelist,mobileArray);

   l1.setAdapter(arrayAdapter);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_namelist);

}

}

activity xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".Namelist">

<ListView
    android:id="@+id/list"
    android:layout_width="409dp"
    android:layout_height="729dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

thanks in advance

raviya97
  • 45
  • 1
  • 12
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx Oct 10 '19 at 16:52

1 Answers1

2

You cannot reference l1 before setContentView(R.layout.activity_namelist)

The right code here:

public class Namelist extends AppCompatActivity {

    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
    ListView l1;
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_namelist);

        l1 = findViewById(R.id.list);

        arrayAdapter = new ArrayAdapter<String>(Namelist.this,android.R.layout.simple_list_item_1,mobileArray);
       l1.setAdapter(arrayAdapter);
    }
}

And you also passed the wrong layout R.layout.activity_namelist to the second parameter of ArrayAdapter. You didn't declare a layout name activity_namelist. So you can use a default build-in layout android.R.layout.simple_list_item_1 ask the code above.