0

I want to create a RecyclerView in Android studio and fill it with my Firestore data in a List Fragment. Here is the Tutorial I used for this:

How to display data from Firestore in a RecyclerView with Android?

The error message:

2020-10-21 20:01:05.074 5691-5691/de.abc.storagekeeper E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.abc.storagekeeper, PID: 5691
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.abc.storagekeeper/de.abc.storagekeeper.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    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)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
    at de.abc.storagekeeper.MainActivity.onCreate(MainActivity.java:45)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

MainActivity:

public class MainActivity extends AppCompatActivity {
    private FirestoreRecyclerAdapter<ProductModel, ProductViewHolder> adapter;

    private FirebaseAnalytics mFirebaseAnalytics;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        RecyclerView recyclerView = findViewById(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //Firebase analytics initializaton
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
        //Firebase Firestore Connection to RecycleView
        FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
        Query query = rootRef.collection("Products")
                .orderBy("id", Query.Direction.ASCENDING);
        FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
                .setQuery(query, ProductModel.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull ProductModel model) {
                ProductModel productModel = new ProductModel();
                holder.setProductName(productModel.getMname());
            }

            @NonNull
            @Override
            public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list, parent, false);
                return new ProductViewHolder(view);
            }
        };
        recyclerView.setAdapter(adapter);

    }
    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (adapter != null) {
            adapter.stopListening();
        }
    }

    private class ProductViewHolder extends RecyclerView.ViewHolder {
        private View view;

        ProductViewHolder(View itemView) {
            super(itemView);
            view = itemView;
        }

        void setProductName(String productName) {
            TextView textView = view.findViewById(R.id.text_View);
            textView.setText(productName);
        }
    }

}

ProductModel:


public class ProductModel {
    private String mid;
    private String msellprice;
    private String mpurchasedprice;
    private String mname;
    private String mdescription;
    private String mdeliverydate;
    private String mquality;

    public ProductModel(){}
    public ProductModel(String id, String sellprice, String purchasedprice, String name, String description, String deliverydate, String quality){
        mid = id;
        msellprice = sellprice;
        mpurchasedprice = purchasedprice;
        mname = name;
        mdescription = description;
        mdeliverydate = deliverydate;
        mquality = quality;
    }



    public String getMid() {
        return mid;
    }
    public void setMid(String name){
        mname = name;
    }
    public String getMsellprice() {
        return mid;
    }
    public void setMsellprice(String name){
        mname = name;
    }
    public String getMpurchasedprice() {
        return mid;
    }
    public void setMpurchasedprice(String name){
        mname = name;
    }
    public String getMname() {
        return mid;
    }
    public void setMname(String name){
        mname = name;
    }
    public String getMdescription() {
        return mid;
    }
    public void setMdescription(String name){
        mname = name;
    }
    public String getMdeliverydate() {
        return mid;
    }
    public void setMdeliverydate(String name){
        mname = name;
    }
    public String getMquality() {
        return mid;
    }
    public void setMquality(String name){
        mname = name;
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.StorageKeeper.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.StorageKeeper.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment_list.xml(where i want to show the data):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".ListFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text_View"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

I'm a beginner and hope you can help me with this.

F1nn-T
  • 67
  • 8

1 Answers1

0

You are getting a NullPointerException, because you are calling .setLayoutManager() on a null object. This is because findViewById() returns null when the View doesn't exist yet. Move this assignment (and the stuff that depends on it) into the onViewCreated() override method:

@Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    RecyclerView recyclerView = findViewById(R.id.list);
    // recyclerView is not null now
}
Boba0514
  • 181
  • 1
  • 3
  • 15