'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference?

Gene Rose Gèrilla :

I'm having an error on Switch in java class and i don't know what i should do

the error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
        at com.example.aclcshsnotifier.AdminDrawer.onCreate(AdminDrawer.java:120)

the code in java class in OnCreate method:

        final Switch sAllStudents = findViewById(R.id.switchStudent);
        final Switch sGrade = findViewById(R.id.switchGrade);
        final Switch sBlock = findViewById(R.id.switchBlock);
        final Switch sHouse = findViewById(R.id.switchHouse);
        final TextView student = findViewById(R.id.txtIndStudent);
        final TextView grade = findViewById(R.id.txtindGrade);
        final TextView block = findViewById(R.id.txtIndBlock);
        final TextView house = findViewById(R.id.txtIndHouse);

        sAllStudents.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (sAllStudents.isChecked()){
                    sGrade.setEnabled(true);
                    sBlock.setEnabled(true);
                    sHouse.setEnabled(true);
                    student.setEnabled(true);
                } else {
                    sGrade.setEnabled(false);
                    sBlock.setEnabled(false);
                    sHouse.setEnabled(false);
                    student.setEnabled(false);
                }
            }
        });

        sGrade.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (sGrade.isChecked()){
                    grade.setEnabled(true);
                } else {
                    grade.setEnabled(false);
                }
            }
        }); //the same format for sBlock and sHouse


xml that has switch:

<Switch
            android:checked="false"
            android:text="All SHS Students"
            android:textSize="18sp"
            android:paddingLeft="20dp"
            android:paddingRight="180dp"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/switchStudent"
            android:background="@drawable/border"
            android:backgroundTint="#80ffffff" app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="60dp"/>

// the other switches that i initialized in java class are the same with the format of switchStudent in xml

what does void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference mean?

Piyush Maheswari :

You should add a null check to the Switch before setting the setOnCheckedChangeListener, like this

Switch s = (Switch) findViewById(R.id.SwitchID);

 if (s != null) {
   s.setOnCheckedChangeListener(this);
 }

This may solve your problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27149&siteId=1