0

Okay I'm trying to check if CheckBox1 and CheckBox2 was checked on Button-Click. I don't understand why it does not work this way:

public class MenuScreen extends ActionBarActivity {

private CheckBox kategorie1;
private CheckBox kategorie2;

public void onClick(View v) {
    if (kategorie1.isChecked() == false && kategorie2.isChecked() == false) {
        Toast.makeText(getBaseContext(), "STOP - You did not check any Checkboxes!", Toast.LENGTH_SHORT).show();
    } else {

        Toast.makeText(getBaseContext(), "GOOD - You're free to go!", Toast.LENGTH_SHORT).show();

    Intent iinent= new Intent(MenuScreen.this,QuizScreen.class);
    startActivity(iinent);
}
}

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

keeps crashing my app with the error message:

Caused by: java.lang.NullPointerException

Hope someone can explain me why it does not work & show me how to do it properly. Thanks in advance!

Blo
  • 11,903
  • 5
  • 45
  • 99
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jeroen Vannevel May 18 '14 at 14:27

1 Answers1

4

Check if your two CheckBox kategorie1 and kategorie2 are properly created, like this :

CheckBox kategorie1 = (CheckBox) findViewById(R.id.checkboxID)

NullPointerException is mostly thrown when this line is not present.

Rogue
  • 751
  • 1
  • 17
  • 36