Skip to content
Advertisement

Android RecyclerView on a null object reference

im trying to get Highscores from a File and display them in a RecyclerView. But im getting the following Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.ausbaustufe1.CustomAdapter.onBindViewHolder(CustomAdapter.java:46)
        at com.example.ausbaustufe1.CustomAdapter.onBindViewHolder(CustomAdapter.java:16)

I know that my scoreItems in

CustomAdapter adapter = new CustomAdapter(scoreItems); 

arent empty. I dont know what i am doing wrong. I´m new in the Android World 🙂

Thanks for helping.

My files: score_activity.java

public class score_activty extends AppCompatActivity {
    ArrayList<ScoreItem> scoreItems;
    FileIOScores io = new FileIOScores(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_activty);
        RecyclerView rvItems = findViewById(R.id.recview);
        scoreItems = io.readScores("file1.txt");
        Log.v("mytag",scoreItems.toString());
        CustomAdapter adapter = new CustomAdapter(scoreItems);
        Log.v("mytag",String.valueOf(adapter.getItemCount()));
        rvItems.setLayoutManager(new LinearLayoutManager(this));
        rvItems.setAdapter(adapter);

    }
}

CustomAdapter.java

package com.example.ausbaustufe1;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

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

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

    private String[] localDataSet;
    private TextView textView;
    private List<ScoreItem> scores;

    public CustomAdapter(ArrayList<ScoreItem> scoreItems) {
        this.scores = scoreItems;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View scoreView = inflater.inflate(R.layout.recycler_style, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(scoreView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ScoreItem scoreItem = scores.get(position);

        // Set item views based on your views and data model
        TextView textView = holder.textView;
        textView.setText(scoreItem.toString());


    }

    @Override
    public int getItemCount() {
        return scores.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;
        private List<ScoreItem> scores;

        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View

            textView = (TextView) view.findViewById(R.id.textView);
        }

        public TextView getTextView() {
            return textView;
        }
        public void CustomAdapter(List<ScoreItem> scores){

            this.scores = scores;
        }





    }
}

activity_score_activity.xml

<?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"
    tools:context=".score_activty">

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/clr"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="-2dp" />
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        />
</LinearLayout>

recycler_style.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="match_parent">
    <TextView
        android:layout_width="@+id/textView"
        android:layout_height="wrap_content"
        />

</LinearLayout>

Advertisement

Answer

You mistype the recycler_style.xml, the id must be specified in the android:id property and not in android:layout_width

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement