mardi 27 avril 2021

java.lang.NPE: Attempt to invoke virtual method 'android.view.View android.widget.CheckBox.findViewById(int)' on a null object reference

Just started learning android development and while trying out the working of a checkbox via the onclicklistener functionality, I was thrown this error. It is on line 23 where I have "chkbx = chkbx.findViewById(R.id.chkbx);

Below is my entire Java code

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity  {

    private CheckBox chkbx, chkbx2, chkbx3;

        @Override
        protected void onCreate (Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            chkbx = chkbx.findViewById(R.id.chkbx);
            chkbx2 = chkbx2.findViewById(R.id.chkbx2);
            chkbx3 = chkbx3.findViewById(R.id.chkbx3);

            chkbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        Toast.makeText(MainActivity.this, "You've watched Lord of the Rings", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(MainActivity.this, "You Should Watch Lord of the Rings", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

}

Here is the xml code:

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

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:text="LOTR"
            android:id="@+id/chkbx"/>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:text="GOT"
            android:layout_toRightOf="@+id/chkbx"
            android:id="@+id/chkbx2"/>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:text="X-Men"
            android:layout_toRightOf="@+id/chkbx2"
            android:id="@+id/chkbx3"/>

</RelativeLayout>

I've read multiple potential bugs but can't seem to apply them in my predicament.

A little help would be great as I have a deadline to create my min projects demonstrating different UI components...

Thank you for your consideration.




Aucun commentaire:

Enregistrer un commentaire