Home > Back-end >  Unable to start activity ComponentInfo and not showing data to recyclerview
Unable to start activity ComponentInfo and not showing data to recyclerview

Time:10-14

I am trying to get data from my Firestore Database and I got this error:

2021-10-12 19:01:18.585 18859-18859/com.dharmik953.mynotes_firebase E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dharmik953.mynotes_firebase, PID: 18859
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dharmik953.mynotes_firebase/com.dharmik953.mynotes_firebase.HomePage}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' 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 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference
        at com.dharmik953.mynotes_firebase.HomePage.onCreate(HomePage.java:66)
        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)

i think there is problem with my one of any layouts or something else i tried to find solution at other sites but nothing changed error remains same. i am very begging stage of my firestore database learning and tis is my very first project i pushed my project on git hub and shared link of it. please help me in this problem.

firestore database:-

"notes"-> uid-> "myNotes"-> noteid-> "title","content".

Github link:-

https://github.com/dharmik953/MyNotes-firebase

{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        binding = ActivityHomePageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        datalist=new ArrayList<>();
        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        layoutManager2 = new LinearLayoutManager(getApplicationContext());
//        layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager2);
        recyclerView.setAdapter(adapter);

        firestore.collection("notes").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                List<DocumentSnapshot> list=queryDocumentSnapshots.getDocuments();
                for(DocumentSnapshot d:list)
                {
                    firebaseModel obj=d.toObject(firebaseModel.class);
                    datalist.add(obj);
                }
                adapter.notifyDataSetChanged();
            }
        });

        firestore = FirebaseFirestore.getInstance();

        addNotes = findViewById(R.id.add_button);

        Objects.requireNonNull(getSupportActionBar()).setTitle("Notes");

        addNotes.setOnClickListener(v -> startActivity(new Intent(HomePage.this,addNotes.class)));

    }

CodePudding user response:

You are getting the following error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference

Because you are trying to use the firestore object before it is initialized. To solve this Exception, please move the following line of code:

firestore = FirebaseFirestore.getInstance();

Right before:

firestore.collection("notes").get().addOnSuccessListener(/* ... /*);

And your error will be gone.

  • Related