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

Getting this error message Unfortunately FunFactsActivity Stopped Working

i am getting these errors inside the LogCat

10-30 14:01:37.030 885-885/com.teamtreehouse.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.teamtreehouse.funfacts, PID: 885 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.teamtreehouse.funfacts/com.teamtreehouse.funfacts.FunFactsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.app.Activity.findViewById(Activity.java:1884) at com.teamtreehouse.funfacts.FunFactsActivity.<init>(FunFactsActivity.java:16) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1208) at android.app.Instrumentation.newActivity(Instrumentation.java:1061) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)             at android.app.ActivityThread.access$800(ActivityThread.java:135)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5017)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)             at dalvik.system.NativeStart.main(Native Method) 10-30 14:01:42.550 885-885/com.teamtreehouse.funfacts I/Process﹕ Sending signal. PID: 885 SIG: 9

Can you post your code? According to the stack trace the error is occuring at line 16 of FunFactsActivity.java.

2 Answers

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

You're calling findViewByID(...) before setContentView(...) has been called.

package com.teamtreehouse.funfacts;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class FunFactsActivity extends Activity {
    private FactRespond newFact = new FactRespond();
    private RelativeLayout newLayout = (RelativeLayout) findViewById(R.id.FunFactsLayout);
    private Colorof nextColor = new Colorof();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_facts);


        final TextView FactLabel = (TextView) findViewById(R.id.textView);
        final Button showFactButton = (Button) findViewById(R.id.button);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                        newLayout.setBackgroundColor(nextColor.getColor());
                        showFactButton.setTextColor(nextColor.getColor());
                      FactLabel.setText(newFact.getFact());
            }
        };
        showFactButton.setOnClickListener(listener);
        Toast.makeText(this,"all Right",Toast.LENGTH_LONG).show();
    }



}

Here's the code of funfacts activity