Home > Enterprise >  Android Java - Receiving null reference error on RecyclerView.setAdapter()
Android Java - Receiving null reference error on RecyclerView.setAdapter()

Time:06-03

I am receiving the following error: error: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference

Here is my code:

PropertyHome.java:

public class PropertyHome extends AppCompatActivity
{
    private RecyclerView recyclerView;
    private RecyclerView.Adapter recyclerAdapter;
    private RecyclerView.LayoutManager recyclerLayoutManager;

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            recyclerView = (RecyclerView)findViewById(R.id.get_property_entry_property_home);

            String[] myRecycler = new String[5];
            myRecycler[0] = "Recycler 1";
            myRecycler[1] = "Recycler 2";
            myRecycler[2] = "Recycler 3";
            myRecycler[3] = "Recycler 4";
            myRecycler[4] = "Recycler 5";

            recyclerAdapter = new GetPropertyRecyclerViewAdapater(myRecycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(recyclerAdapter);
        }
        catch(Exception e)
        {
            Toast.makeText(PropertyHome.this, "Error: "   e.toString(), Toast.LENGTH_LONG).show();
        }
    }
    ...
}

activity_property_homr.xml:

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

<ScrollView 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="wrap_content"
    tools:layout_editor_absoluteX="1dp"
    tools:layout_editor_absoluteY="1dp"
    tools:context=".MainActivity">

        <LinearLayout
            android:id="@ id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:onClick="AppLogout"
            android:orientation="vertical"
            android:textAlignment="gravity" >

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@ id/get_property_entry_property_home"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:itemCount="0" />
        </LinearLayout>
</ScrollView>

Layout for displaying each row of the RecyclerView:

GetPropertyRecyclerViewAdapter.java:


public class GetPropertyRecyclerViewAdapater extends RecyclerView.Adapter<GetPropertyRecyclerViewAdapater.ViewHolder>
{
    private String[] dataSet;

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public final TextView mTextView;

        public ViewHolder(View v)
        {
            super(v);

            mTextView = (TextView)v.findViewById(R.id.location_property_home);
        }

        public TextView getTextView()
        {
            return mTextView;
        }
    }

    public GetPropertyRecyclerViewAdapater(String[] myDataSet)
    {
        dataSet = myDataSet;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_item, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position)
    {
        holder.getTextView().setText(dataSet[position]);
    }

    @Override
    public int getItemCount()
    {
        return dataSet.length;
    }
}

recycler_view_item.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="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

  <TextView
      android:id="@ id/textView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="20dp"
      android:layout_marginTop="20dp"
      android:text="Price:"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@ id/image_property_home" />

  <TextView
      android:id="@ id/price_property_home"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="10dp"
      android:text="0"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="@ id/textView"
      app:layout_constraintStart_toEndOf="@ id/textView"
      app:layout_constraintTop_toTopOf="@ id/textView" />

  <TextView
      android:id="@ id/textView6"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="20dp"
      android:layout_marginTop="10dp"
      android:text="Location:"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@ id/textView" />

  <TextView
      android:id="@ id/location_property_home"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="10dp"
      android:text="N/A"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="@ id/textView6"
      app:layout_constraintStart_toEndOf="@ id/textView6"
      app:layout_constraintTop_toTopOf="@ id/textView6" />

  <ImageView
      android:id="@ id/imageView"
      android:layout_width="30dp"
      android:layout_height="30dp"
      android:layout_marginStart="20dp"
      android:layout_marginTop="20dp"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@ id/location_property_home"
      app:srcCompat="@drawable/bedroom" />

  <TextView
      android:id="@ id/bedrooms_property_home"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="10dp"
      android:text="0"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="@ id/imageView"
      app:layout_constraintStart_toEndOf="@ id/imageView"
      app:layout_constraintTop_toTopOf="@ id/imageView" />

  <ImageView
      android:id="@ id/imageView2"
      android:layout_width="30dp"
      android:layout_height="30dp"
      android:layout_marginStart="20dp"
      android:layout_marginTop="20dp"
      app:layout_constraintStart_toEndOf="@ id/bedrooms_property_home"
      app:layout_constraintTop_toBottomOf="@ id/location_property_home"
      app:srcCompat="@drawable/bathroom" />

  <TextView
      android:id="@ id/bathrooms_property_home"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="10dp"
      android:text="0"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="@ id/imageView2"
      app:layout_constraintStart_toEndOf="@ id/imageView2"
      app:layout_constraintTop_toTopOf="@ id/imageView2" />

  <ImageView
      android:id="@ id/imageView3"
      android:layout_width="30dp"
      android:layout_height="30dp"
      android:layout_marginStart="20dp"
      android:layout_marginTop="20dp"
      app:layout_constraintStart_toEndOf="@ id/bathrooms_property_home"
      app:layout_constraintTop_toBottomOf="@ id/location_property_home"
      app:srcCompat="@drawable/garage" />

  <TextView
      android:id="@ id/garages_property_home"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="10dp"
      android:text="0"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="@ id/imageView3"
      app:layout_constraintStart_toEndOf="@ id/imageView3"
      app:layout_constraintTop_toTopOf="@ id/imageView3" />

  <ImageView
      android:id="@ id/imageView4"
      android:layout_width="30dp"
      android:layout_height="30dp"
      android:layout_marginStart="20dp"
      android:layout_marginTop="20dp"
      app:layout_constraintStart_toEndOf="@ id/garages_property_home"
      app:layout_constraintTop_toBottomOf="@ id/location_property_home"
      app:srcCompat="@drawable/area" />

  <TextView
      android:id="@ id/area_property_home"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="10dp"
      android:text="0"
      android:textColor="#000000"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="@ id/imageView4"
      app:layout_constraintStart_toEndOf="@ id/imageView4"
      app:layout_constraintTop_toTopOf="@ id/imageView4" />

  <ImageView
      android:id="@ id/image_property_home"
      android:layout_width="wrap_content"
      android:layout_height="200dp"
      android:layout_marginStart="20dp"
      android:layout_marginTop="20dp"
      android:layout_marginEnd="20dp"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:srcCompat="@drawable/placeholder_image" />

  <ImageButton
      android:id="@ id/edit_property_home"
      android:layout_width="48dp"
      android:layout_height="48dp"
      android:scaleType="center"
      app:layout_constraintEnd_toEndOf="@ id/image_property_home"
      app:layout_constraintTop_toTopOf="@ id/image_property_home"
      app:srcCompat="@android:drawable/ic_menu_edit" />

  <ImageButton
      android:id="@ id/delete_property_home"
      android:layout_width="48dp"
      android:layout_height="48dp"
      app:layout_constraintEnd_toStartOf="@ id/edit_property_home"
      app:layout_constraintTop_toTopOf="@ id/image_property_home"
      app:srcCompat="@android:drawable/ic_menu_delete" />

</androidx.constraintlayout.widget.ConstraintLayout>

I've tried my best to find where the error is, but cannot find it.

CodePudding user response:

You missed an important part in your code, setContentView() add it in you onCreate() right after super() call Should be working then

public class PropertyHome extends AppCompatActivity
{
    private RecyclerView recyclerView;
    private RecyclerView.Adapter recyclerAdapter;
    private RecyclerView.LayoutManager recyclerLayoutManager;

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_homr); // add setContentView here
        try {
            recyclerView = (RecyclerView)findViewById(R.id.get_property_entry_property_home);

            String[] myRecycler = new String[5];
            myRecycler[0] = "Recycler 1";
            myRecycler[1] = "Recycler 2";
            myRecycler[2] = "Recycler 3";
            myRecycler[3] = "Recycler 4";
            myRecycler[4] = "Recycler 5";

            recyclerAdapter = new GetPropertyRecyclerViewAdapater(myRecycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(recyclerAdapter);
        }
        catch(Exception e)
        {
            Toast.makeText(PropertyHome.this, "Error: "   e.toString(), Toast.LENGTH_LONG).show();
        }
    }
    ...
}
  • Related