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

Accessing Strings.xml from Factbook declaring an array.

I have a fatal exception when trying to innitialize the app. java.lang.ExceptionInInitializerError caused by java.lang.NullPointerException

This is my FunFactsActivity

package pe.Anastomosis.funfacts;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
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 {

    public  static  final String TAG = FunFactsActivity.class.getSimpleName();


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

        // Declare our View variables and assign the Views from the layout file
        final TextView factlabel = (TextView) findViewById(R.id.FactTextView);
        final Button showFactButton = (Button) findViewById(R.id.BotonMostrarFact);
        final RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);


        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fact = FactBook.getFact();
                //Update the label with our dynamic fact
                factlabel.setText(fact);

                int color = ColorWheel.getColor();
                mRelativeLayout.setBackgroundColor(color);
                showFactButton.setTextColor(color);
            }
        };
        showFactButton.setOnClickListener(listener);
    }
}

This is my Factbook

package pe.Anastomosis.funfacts;

import android.content.res.Resources;

import java.util.Random;

/**
 * Created by horacio on 2/06/15.
 */
public class FactBook {

    //member variable (propierties about the object)

    public static   Resources res;
    public static   String[] mfacts = res.getStringArray(R.array.Funfacts);

    // Method (abilities:things the object can do)

    public static String getFact() {

        String fact = "";

        // Randomly select a fact
        Random randomGenerator = new Random(); // construct a new random number generator
        Random randomMeGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(mfacts.length);
        int randomMe = randomMeGenerator.nextInt(20);      

        //Ramdom "hi"
        if (randomMe == 5){
            fact = "Hi";
        }
        else {
            //Utiliza el valor de randomNumber para asignar del array a fact
            fact = mfacts[randomNumber];
        }
        return fact;
    }
}

And my strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Fun Facts</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string-array name="Funfacts">
        <item>Ants stretch when they wake up in the morning.</item>
        <item>Ostriches can run faster than horses</item>
        <item>Olympic gold medals are actually made mostly of silver</item>
        <item>You are born with 300 bones; by the time you are an adult you will have 206.</item>
        <item>It takes about 8 minutes for light from the Sun to reach Earth.</item>
        <item>Some bamboo plants can grow almost a meter in just one day.</item>
        <item>The state of Florida is bigger than England.</item>
        <item>Some penguins can leap 2-3 meters out of the water.</item>
        <item>On average, it takes 66 days to form a new habit.</item>
        <item>Mammoths still walked the earth when the Great Pyramid was being built.</item>
        <item>Treehouse is not actually in a tree.</item>
    </string-array>

</resources>

And my log

06-13 19:43:59.721    2324-2324/pe.Anastomosis.funfacts E/AndroidRuntime FATAL EXCEPTION: main
    Process: pe.Anastomosis.funfacts, PID: 2324
    java.lang.ExceptionInInitializerError
            at pe.Anastomosis.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:32)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at pe.Anastomosis.funfacts.FactBook.<clinit>(FactBook.java:16)
            at pe.Anastomosis.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:32)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Thank you very much in advance

2 Answers

Well on line 32 in your FunFactsActivity you're trying to access the facts directly from the class. That causes the exception. Instead, try instantiating the class first, and then use the created object to get facts. Something like this:

FactBook factBook = new FactBook();
factlabel.setText(factBook.getFact()); // I would recommend renaming the label to "factLabel" (camelCase)

Thanks, but i still have this error log even with your changes.

06-14 12:47:49.145  10231-10231/pe.Anastomosis.funfacts E/AndroidRuntime FATAL EXCEPTION: main
    Process: pe.Anastomosis.funfacts, PID: 10231
    java.lang.ExceptionInInitializerError
            at pe.Anastomosis.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:32)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at pe.Anastomosis.funfacts.FactBook.<clinit>(FactBook.java:15)
            at pe.Anastomosis.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:32)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)