0

My code is below, I'm thinking it has something to do with my if / else statement. Essentially I want to make sure there is at least one radio button in a group checked before I open the next page. Once this works, I'll set it up so that if one of the radiobuttons in the group isn't checked then It will display an error message.

    public void onClickListenerButton() {
    radioGroup_gender = (RadioGroup) findViewById(R.id.radioGroup_gender);
    radioGroup_course = (RadioGroup) findViewById(R.id.radioGroup_course);
    button_splits = (Button)findViewById(R.id.button_splits);

    button_splits.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.idealsplit.admin.idealsplit.ResultsActivity");
                    int selectedID_gender = radioGroup_gender.getCheckedRadioButtonId();
                    int selectedID_course = radioGroup_course.getCheckedRadioButtonId();
                    radioButton_gender = (RadioButton) findViewById(selectedID_gender);
                    radioButton_course = (RadioButton) findViewById(selectedID_course);

                    CharSequence gender, courseType, age, event, time;

                    if (selectedID_gender == -1) {
                        //Throw Error
                        isNull = true;
                    } else if (selectedID_course == -1) {
                        //Throw Error
                        isNull = true;
                    } else {
                        courseType = radioButton_course.getText();
                        gender = radioButton_gender.getText();
                        startActivity(intent);

                    }
                }

            }
    );
}

And here is the logcat:

03-01 08:52:22.060 19234-19234/com.idealsplit.admin.idealsplit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.idealsplit.admin.idealsplit, PID: 19234
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idealsplit.admin.idealsplit/com.idealsplit.admin.idealsplit.ResultsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5294)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                 at com.idealsplit.admin.idealsplit.ResultsActivity.onCreate(ResultsActivity.java:96)
                                                                                 at android.app.Activity.performCreate(Activity.java:5990)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5294) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

Thanks for any and all help with this, if more information was needed let me know!

**If it helps I was following this youtube tutorial: https://www.youtube.com/watch?v=3f0NAn5xFy4

matt3948
  • 3
  • 2
  • 1
    See https://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Harry Mar 01 '16 at 15:04
  • Probably the actual problem is calling `findViewById()` in the wrong place and you should have a look at e.g. [this discussion](http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate). – Markus Kauppinen Mar 01 '16 at 15:33

1 Answers1

3

It looks like button_splits is null so (Button)findViewById(R.id.button_splits); is returning null.

Ensure that view R.id.button_splits exists. Your view that is attached to this activity must have a button on it with the id of 'button_splits'.

What is a Null Pointer Exception

also you can stop this NPE happening by ensuring the button is not null before adding the listener, but if the button is null then this will not add the listener.

if (button_splits != null) {
    button_splits.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.idealsplit.admin.idealsplit.ResultsActivity");
                    int selectedID_gender = radioGroup_gender.getCheckedRadioButtonId();
                    int selectedID_course = radioGroup_course.getCheckedRadioButtonId();
                    radioButton_gender = (RadioButton) findViewById(selectedID_gender);
                    radioButton_course = (RadioButton) findViewById(selectedID_course);

                    CharSequence gender, courseType, age, event, time;

                    if (selectedID_gender == -1) {
                        //Throw Error
                        isNull = true;
                    } else if (selectedID_course == -1) {
                        //Throw Error
                        isNull = true;
                    } else {
                        courseType = radioButton_course.getText();
                        gender = radioButton_gender.getText();
                        startActivity(new Intent(getApplicationContext(), ResultsActivity.class));
                        startActivity(intent);

                    }
                }

            }
    );
}
J0B
  • 1,648
  • 1
  • 12
  • 24