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 trialJonas Grønbek
403 Points''My application has stopped'' code and crash error as comment, please help!
--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 2472 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.myapplication.FunFactsActivity$1.onClick(FunFactsActivity.java:29) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Application terminated.
1 Answer
jemartin
22,295 PointsThe following line declares a TextView instance, but since you never assign it, the object remains null. private TextView FactsTextView;
In your OnClickListener, you attempt to assign some text to this object. FactsTextView.setText(fact);
It looks like the problem is that you declare two fields (FactsTextView and mShowFact) in your class, but instead of initializing those in your onCreate method, you instead initialize two new local variables (newFactTextView and mShowFactButton).
Jonas Grønbek
403 PointsJonas Grønbek
403 Pointspublic class FunFactsActivity extends AppCompatActivity { // Declare our viewvariables private TextView FactsTextView; private Button mShowFact;