java.lang.NullPointerException: mainRvRef must not be null ,where mainRvRef is referencing a reyclerView

Solution 1:

I suggest you to use viewbinding in kotlin. It will easy your id reference. If you use viewbinding than the code will look like this

class HomeActivity:AppCompatActivity() {

    private lateinit var binding: ActivityHomeBinding
    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)
        binding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        Log.v("Question", questions[0].solution[0])
        binding.mainRvRef.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)

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

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

change your id according to the id name

UPDATE

You need to add these line in your build.gradle

buildFeatures {
        viewBinding true
    }

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)
        }

    }
}