skip to Main Content

Am having problem while passing my arrayAdapter the ListViewAdapter in android using kotlin.
The error message java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ListView.setAdapter(android.widget.ListAdapter)‘ on a null object reference indicates that I am trying to call a method on an object reference which is null.

Here is my mainActivity:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val lv = findViewById<ListView>(R.id.myList)
        val array = arrayOf("test", "test", "test", "test", "test")
        val myAdapter: ArrayAdapter<String> =
            ArrayAdapter(this, android.R.layout.simple_list_item_1, array)
        lv.adapter = myAdapter
    }
}

Here is the Layout file:

<?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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingHorizontal="20dp"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp" />
</LinearLayout>

And here is the error that I got

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at com.example.myapplication5.MainActivity.onCreate(MainActivity.kt:16)
        at android.app.Activity.performCreate(Activity.java:7994)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2022-12-25 14:33:52.075 14422-14438/com.example.myapplication5 W/System: A resource failed to call close. 

Debug shows that myAdapter contains corretly the array values

enter image description here

2

Answers


  1. You have to call setContentView(<Name of your layout file>) before accessing any views. See working example (in my case the name of the layout file is activity_main.xml):

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            val lv = findViewById<ListView>(R.id.myList)
            val array = arrayOf("test", "test", "test", "test", "test")
            val myAdapter: ArrayAdapter<String> =
                ArrayAdapter(this, android.R.layout.simple_list_item_1, array)
            lv.adapter = myAdapter
        }
    }
    
    Login or Signup to reply.
  2. You are passing ArrayAdapter where in error you can check it needs ListAdapter.

    Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search