Search code examples
javaandroidxmlandroid-arrayadaptervirtual

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object refer


I am trying to make a single screen simple app for displaying listview but this error occurred, ReportCard is the ArrayAdapter class.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity" />

MainActivity.java

package com.vitikasoni.reportcard;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        ArrayList<Student> s = new ArrayList<Student>();
        s.add(new Student("Vitika", 87, 79, 93, 89));
        s.add(new Student("Faizal", 90, 95, 89, 99));
        s.add(new Student("Prince", 83, 73, 92, 67));
        s.add(new Student("Sejal", 83, 79, 95, 79));

        ReportCard r = new ReportCard(this, s);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(r);
    }
}

view_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/na"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/m"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/e"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="English:98" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/ev"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="EVS:98" />

        <TextView
            android:id="@+id/h"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hindi:98" />

        <TextView
            android:id="@+id/p"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Percent:98%" />
    </LinearLayout>
 </LinearLayout>

Reportcard.java

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

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

import java.util.ArrayList;

public class ReportCard extends ArrayAdapter<Student> {
    public ReportCard(Context c, ArrayList<Student> s) {
        super(c, 0, s);
    }

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.activity_main, parent, false);
        }
        Student currentWord = getItem(position);

        TextView na = (TextView) listItemView.findViewById(R.id.na);
        na.setText(currentWord.getName());

        TextView m = (TextView) listItemView.findViewById(R.id.m);
        m.setText(currentWord.getMaths());

        TextView h = (TextView) listItemView.findViewById(R.id.h);
        h.setText(currentWord.getHindi());

        TextView e = (TextView) listItemView.findViewById(R.id.e);
        e.setText(currentWord.getEng());

        TextView ev = (TextView) listItemView.findViewById(R.id.ev);
        ev.setText(currentWord.getEvs());

        TextView p = (TextView) listItemView.findViewById(R.id.p);

        return listItemView;
    }
}

Student.java

package com.vitikasoni.reportcard;

public class Student {
    private String name;
    private int eng;
    private int maths;
    private int hindi;
    private int evs;
    private double per;

    public Student(String n, int e, int m, int h, int ev) {
        name = n;
        eng = e;
        maths = m;
        hindi = h;
        evs = ev;
    }

    public String getName() {
        return name;
    }

    public int getEng() {
        return eng;
    }

    public int getHindi() {
        return hindi;
    }

    public int getEvs() {
        return evs;
    }

    public int getMaths() {
        return maths;
    }

    public double getPer() {
        double pr = (eng + hindi + maths + evs) / 4;
        return pr;
    }
}

Solution

  • First set correct layout id for ListView adapter to overcome Null Pointer Exception which you currently have. After fix this your code won't run because there is another problem and you definitely get android.content.res.Resources$NotFoundException: String resource ID #0x4f exception.

    Problem at your setText(currentWord.getMaths()) line because here currentWord.getMaths() is integer type and setText(integer) considers this interger paramer as R.string.someName.

    So either change private int maths; to private String maths; You have to change all interger type to String type.

    OR

    use below code in getView() method of Adapter.

    // Here I am concating integer to String and setting to TextView

            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View listItemView = convertView;
            if (listItemView == null) {
                listItemView = LayoutInflater.from(getContext()).inflate(
                        R.layout.view_view, parent, false);
            }
            Student currentWord = getItem(position);
    
            TextView na = (TextView) listItemView.findViewById(R.id.na);
            na.setText(currentWord.getName());
    
            TextView m = (TextView) listItemView.findViewById(R.id.m);
            m.setText(currentWord.getMaths()+"");
    
            TextView h = (TextView) listItemView.findViewById(R.id.h);
            h.setText(currentWord.getHindi()+"");
    
            TextView e = (TextView) listItemView.findViewById(R.id.e);
            e.setText(currentWord.getEng()+"");
    
            TextView ev = (TextView) listItemView.findViewById(R.id.ev);
            ev.setText(currentWord.getEvs()+"");
    
            TextView p = (TextView) listItemView.findViewById(R.id.p);
    
            return listItemView;
        }