can anyone help me out why I am getting null value even there is no error in the code

package com.example.myquizapp
import android.os.Bundle
import android.util.Log
import android.widget.ProgressBar
import android.widget.TextView

here is my code

class QuizActivity : AppCompatActivity() {
    private var progressBar : ProgressBar?= null
    private var tvProgress : TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) { 
            progressBar = findViewById(  

on hover I get :public static final int progressBar = 1000004

            tvProgress = findViewById(

// public static final int tv_progress = 100000

//on hover progressBar I get : private final var progressBar: ProgressBar?


            var currentPosition = 1
            Log.e("currentPosition" "${currentPosition})        // 1
            Log.e("ProgressBar", "${progressBar}")    // null, getting null value on logcat
            Log.e("TvOrogress", "${tvProgress}")// null, getting null value on logcat
            Log.e("tvProgress",${tvProgress?.text.toString}  // null


>Solution :

Move the findViewById() calls after setContentView().

setContentView() inflates the view hierarchy and adds it to the activity. findViewById() looks up view in the activity view hierarchy, and it is empty before setContentView() so findViewById() returns null.

Leave a Reply