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 Build a Simple Android App Basic Android Programming Adding the OnClick Method

''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.

public class FunFactsActivity extends AppCompatActivity { // Declare our viewvariables private TextView FactsTextView; private Button mShowFact;

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

    // Assign the views from the layout to the corresponding variables
    TextView newFactTextView = (TextView) findViewById(R.id.Facts);
    Button mShowFactButton = (Button) findViewById(R.id.newFact);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         // Button comment whatever update with new fact
            String fact = "Ostriches can run faster than horses";
            FactsTextView.setText(fact);
        }
    };
    mShowFactButton.setOnClickListener(listener);
}

1 Answer

The 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).