0

I am using a CheckBox in my activity layout. But I don't know why I am not getting the refrence of the CheckBox. It shows NullPointerException everytime. I have used another checkbox in my dialog fragment but it was working fine. I don't know what is the cause of NullPointerException.

Here is xml code

 <CheckBox
        android:id="@+id/ccb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        android:text="Add Your Previous Amount\n with Current Amount "
        android:textColor="@color/batteryChargedBlue"
        android:textSize="13dp"
        android:fontFamily="@font/caviardreams_bolditalic"
        />

Here is the java code where I am using the checkbox and getting NullPointerException.

public class StartActivity extends AppCompatActivity {



//Declaring the CheckBox variable globally


CheckBox scb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    ...

       //Initializing in the onCreate() method
       scb = (CheckBox)findViewById(R.id.ccb);

    ...
}

//using it in the button clicked method to check whether checkbox is checked or not
public void setInitialMonthlyAmount(View view) {

      if(scb.isChecked()) { //Getting The NullPointerException Here Don't know why ??
                System.out.println("Checkbox is checked");
                //Code
            }else {
                //Code
            }
}


}

I think everything is correct. and I am using the same way to get the references of other views like TextViews, EditText etc and they are working fine. I don't what is wrong with this checkbox. Please Help!!!

Noman khanbhai
  • 551
  • 7
  • 16
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Martin Zeitler Sep 24 '19 at 19:21
  • Likely you're calling `.setInitialMonthlyAmount()` too soon. – Martin Zeitler Sep 24 '19 at 19:22
  • Did you try debugging and checking what's the effect of this line? `scb = (CheckBox)findViewById(R.id.ccb);` – Krystian G Sep 24 '19 at 19:30
  • I am initialising the checkbox in onCreate() method. And after that i am calling the setInitialMonthlyAmount().@MartinZeitler – Noman khanbhai Sep 24 '19 at 19:53
  • Just before and after this line 'scb = (CheckBox)findViewById(R.id.ccb);' I am printing the value of scb and it gives null in both the case. I am using other views also in the same way but they working fine. I dont know what's wrong with this.@KrystianG – Noman khanbhai Sep 24 '19 at 19:57

2 Answers2

1

Are you sure you coded Activity like this:?

public class StartActivity extends AppCompatActivity {

CheckBox scb;

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

    scb = (CheckBox)findViewById(R.id.ccb);
    setInitialMonthlyAmount(view)
}

public void setInitialMonthlyAmount(View view) {

  if(scb.isChecked()) { 
            System.out.println("Checkbox is checked");
            //Code
        }else {
            //Code
        }

}
Unaisul Hadi
  • 620
  • 1
  • 7
  • 22
0

Make sure you are calling setContentView(R.layout.your_layout_with_checkbox); in your activitie's onCreate !

Thalis Vilela
  • 335
  • 1
  • 10
  • I am calling setContentView(R.layout.activity_start);. Did you mean I should call it like this setContentView(R.id.activity_start_with_checkbox): or something other than that. @ThalisVilela – Noman khanbhai Sep 25 '19 at 04:02