Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Melodie 1
Melodie 1
3,931 Points

Why does this app always crash?

https://github.com/Melodie21/Android-app

Each time i choose an answer (check a radio button ) , and submit it , the app crashes. can someone help me figure out why is that? Is it because of the if statement inside of the onClick(View v ) method or something else ? I would really appreciate any tips or advice :)

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

It's important to realize when you assign a variable to the result of a method or expression that can change over time, you are only assigning it the value at that moment. The if the result changes, the variable won't change with it, unless you reassign the variable the new result.

In your OnClickListener you are using a RadioButton that you intend to be the currently selected one. However instead of grabbing the currently selected button in the listener, you assign the value in onCreate() which sets it to the RadioButton selected when the app starts. Since no button is selected (and you aren't checking if the submit button is being pressed without giving an answer) the RadioButton is null causing the app to crash when you try to access it.

Melodie 1
Melodie 1
3,931 Points

Thanks a lot :)