Home > Blockchain >  java.lang.NullPointerException: mainRvRef must not be null ,where mainRvRef is referencing a reycler
java.lang.NullPointerException: mainRvRef must not be null ,where mainRvRef is referencing a reycler

Time:01-14

I referenced mainRvRef using findViewById, but still I am getting the error as it can't bew null. I don't think it as a normal NullPointerException , please help me out !

here's my HomeActivity class

package com.example.loginkt


import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton

class HomeActivity:AppCompatActivity() {
    var questions = ArrayList<QuestionFB>()
    init {
        for(i:Int in 1..10){
            questions.add(QuestionFB(-1,"cse $i","krishna $i","$i. multiply $i * ${i*i}","maths $i", listOf("s","Sas")))
        }
    } // for testing
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        val fabR = findViewById<FloatingActionButton>(R.id.addQnFab)
        val mainRvRef = findViewById<RecyclerView>(R.id.mainRv)
        Log.v("Question", questions[0].solution[0])
        mainRvRef.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)

        // ABOVE LINE MAKE IT'S SHOWING "mainRvRef can't be null"

        mainRvRef.adapter = MainAdapters(questions,this)
        fabR.setOnClickListener{
            val intent = Intent(this, AskActivity::class.java)
            startActivity(intent)
        }
    }
}

and Here's my activity_home.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="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@ id/addQnFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        android:backgroundTint="@color/pBlue_2"
        android:focusable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@drawable/add_icons"
        app:maxImageSize="50dp"
        android:contentDescription="@string/add_question" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/mainRv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

and here's my stack trace

V/Question: s
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.loginkt, PID: 7231
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginkt/com.example.loginkt.HomeActivity}: java.lang.NullPointerException: mainRvRef must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: mainRvRef must not be null
        at com.example.loginkt.HomeActivity.onCreate(HomeActivity.kt:25)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        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:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
I/Process: Sending signal. PID: 7231 SIG: 9

I don't know why this error coming as I initialized mainRvRef as usual, and kindly ignore my bad English!

CodePudding user response:

I suggest you to use enter image description here

after you need to sync and rebuild your project. And this is your mainactivity.kt example

package com.example.loginkt

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.loginkt.databinding.ActivityMainBinding
import com.google.firebase.auth.FirebaseAuth


lateinit var emailG: String
lateinit var passG: String
lateinit var auth: FirebaseAuth

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private fun firebaseLogin(email: String, pass: String) {
        if (email != "" || pass != "") {
            auth.signInWithEmailAndPassword(email, pass).addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    getUser()
                    val intent = Intent(this, HomeActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            }.addOnFailureListener { exception ->
                Toast.makeText(applicationContext, exception.localizedMessage, Toast.LENGTH_LONG)
                    .show()
            }
        } else {
            Toast.makeText(applicationContext, "Enter all the fields", Toast.LENGTH_LONG).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        auth = FirebaseAuth.getInstance()
        title = "Login"
        binding = ActivityMainBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.SupTB.setOnClickListener {
            val intent = Intent(this, RegisterActivity::class.java)
            startActivity(intent)
        }
        binding.loginBtn.setOnClickListener {
            emailG = binding.emailTxt.text.toString()
            passG = binding.passwordTxt.text.toString()
            firebaseLogin(emailG, passG)
        }

    }
}

CodePudding user response:

It's actually my careless fault ,my dumb, I forget to update my activity_home.xml in v24 :|

mainRvref is not referencing to anything in layout_v24 xml file, so check all your layout files

  •  Tags:  
  • Related