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

Luke Kim
Luke Kim
482 Points

The app won't open

Here is my code: package com.example.overmind.funfacts2;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class FunFactsactivity extends AppCompatActivity { //Declare our View variables private TextView mFactTextView; private Button mShowFactButton;

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

    // Assign the Views from the layput file to the corresponding variables
    mFactTextView = (TextView) findViewById(R.id.FacttextView);
    mShowFactButton = (Button) findViewById(R.id.ShowFactButton);
    View.OnClickListener listen = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String fact = "Ostriches can run faster than horses.";
            mFactTextView.setText(fact);
        }
    };
    mShowFactButton.setOnClickListener(listen);
}

}

And here is the error:10/13 21:23:02: Launching app $ adb push C:\Users\Overmind\AndroidStudioProjects\FunFacts2\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.overmind.funfacts2 $ adb shell pm install -r "/data/local/tmp/com.example.overmind.funfacts2" Success

$ adb shell am start -n "com.example.overmind.funfacts2/com.example.overmind.funfacts2.FunFactsactivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Client not ready yet..Connected to process 2185 on device emulator-5554 W/System: ClassLoader referenced unknown path: /data/app/com.example.overmind.funfacts2-2/lib/x86 I/InstantRun: Instant Run Runtime started. Android package is com.example.overmind.funfacts2, real application class is null. W/System: ClassLoader referenced unknown path: /data/app/com.example.overmind.funfacts2-2/lib/x86 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable D/AndroidRuntime: Shutting down VM

              --------- beginning of crash

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.overmind.funfacts2, PID: 2185 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.overmind.funfacts2/com.example.overmind.funfacts2.FunFactsactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.overmind.funfacts2.FunFactsactivity.onCreate(FunFactsactivity.java:29) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

The app will not open. If you have completed this already, can you tell me what you did and what I did wrong. Also can you post your code?

2 Answers

Seth Kroger
Seth Kroger
56,414 Points

Let's take a look at the relevant part of the crash log:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.overmind.funfacts2.FunFactsactivity.onCreate(FunFactsactivity.java:29)

I tells you the error causing the crash, a NullPointerException, and the last bit gives you the file and line number of the code where that happened, line 29 of FunFactsactivity.java:

mShowFactButton.setOnClickListener(listen);

NullPointerException essentially means that a variable wasn't set correctly, so it's referring to nothing (or null) instead of an object. Since mShowFactButton is supposed to be set here:

mShowFactButton = (Button) findViewById(R.id.ShowFactButton);

it means the findViewById() method failed which in turn suggests an issue in the XML layout file like an id not matching up.

Luke Kim
Luke Kim
482 Points

Thanks man for helping me out. Really appreciate the support