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 (2014) Coding the Fun Facts Properties of Arrays

Adam Nwaozo
Adam Nwaozo
370 Points

Help! facts.length is giving me a red error message once I type it in Android Studio.

Help! facts.length is giving me a red error message once I type it in Android Studio.

See my code:

public class FunFactsActivity extends Activity {

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

    // Declare our view variables and assign views from the layout file
    final TextView factlabel = (TextView) findViewById(R.id.textView3);
    Button showFactButton = (Button) findViewById(R.id.button);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String[] facts = {
                              "Quote 1",
                              "Quote 2",
                              "Quote 3",
                              "Quote 4",
                              "Quote 5",
                              "Quote 6",
                              "Quote 7",
                              "Quote 8",
                              "Quote 9",
                              "Quote 10"};
        }

        //The button was clicked
            String Fact = "";
        // Randomly Choose a fact
            Random randomGenerator = new Random(); //Generates new random number
            int randomNumber =randomGenerator.nextInt(facts.length);

            fact = Facts [randonNumber];

             // Update fact label

            factlabel.setText(Fact);
        }
    };
    showFactButton.setOnClickListener(listener);

1 Answer

You declared the fact string with uppercase F and you called it with lowercase f , and it's better to declare your array outside the onClick method