Skip to content
Advertisement

I am getting an runtime error (Null Pointer Exception) while using ListView in Android Studio

I am making a Task Manager app using ListView to Show all the tasks.But I am getting this error

    Process: com.example.taskmanager, PID: 29293
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmanager/com.example.taskmanager.MainActivity}: 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:2364)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426)
        at android.app.ActivityThread.access$900(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5318)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)
     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.taskmanager.MainActivity.onCreate(MainActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:6024)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2426) 
        at android.app.ActivityThread.access$900(ActivityThread.java:155) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5318) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717) 

what I think the error is that, I am sending wrong context in my custom adapter in MainActivity.java file

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.MyAdapter.MyAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<DataClass> datalist= new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();

        DataClass data1 = new DataClass();
        data1.setId(1);
        data1.setStatus(0);
        data1.setTask("yo yo yo");

        datalist.add(data1);
        datalist.add(data1);
        datalist.add(data1);
        datalist.add(data1);
        datalist.add(data1);


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

        MyAdapter myadapter = new MyAdapter(getApplicationContext(),R.layout.new_task_layout,datalist);

        taskListView.setAdapter(myadapter);

    }
}

My custom Adapter is

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.taskmanager.Model.DataClass;
import com.example.taskmanager.R;

import java.util.List;

public class MyAdapter extends ArrayAdapter<DataClass> {
    Context mcontext;
    int lytresourceId;
    List<DataClass> data;

    public MyAdapter(@NonNull Context context, int resource, @NonNull List<DataClass> objects) {
        super(context, resource, objects);
        this.mcontext=context;
        this.lytresourceId=resource;
        this.data=objects;
    }

    @Nullable
    @Override
    public DataClass getItem(int position) {
        return super.getItem(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View row, @NonNull ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mcontext);
        row = inflater.inflate(lytresourceId,parent,false);

        CheckBox checkbox;
        checkbox=row.findViewById(R.id.checkBox);

        DataClass mdata = data.get(position);
        checkbox.setText(mdata.getTask());

        return row;
    }
}

My Xml file for activity Main is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/TaskText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="Task"
            android:textColor="#000000"
            android:textSize="40sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/LinearLayout1"
        />
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="20dp"
        app:srcCompat="@android:drawable/ic_input_add" />

</RelativeLayout>

My custom layout file is

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardElevation="3dp"
    app:cardCornerRadius="8dp"
    android:layout_marginHorizontal="4dp"
    android:layout_marginVertical="4dp">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="task1"
            android:padding="4dp"/>
    </RelativeLayout>
</androidx.cardview.widget.CardView>

My Data class is

package com.example.taskmanager.Model;

public class DataClass {
    private int id,status;
    private String task;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }
}

I am also using a Splash Screen but, I don’t think that is a problem because my app is crashing after the splash screen is over, and when the intent goes to MainActivity. Can someone please tell me the error Also, I don’t think there are any Dependencies missing from my File.

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Advertisement

Answer

You are getting this error because setContentView(R.layout.layoutname) is missing in MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // this is missing in your class 
        setContentView(R.layout.activity_main);
  }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement