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

Android Project > Java code ".length" cannot be resolved or is not a field in "nextInt(answers.length)" crystal ball project

package com.example.crystalball;

import java.util.Random;

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

public class MainActivity extends Activity {

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

        // Declare our View variables and assign them the Views from layout file
        final TextView answerLabel = (TextView) findViewById(R.id.textView1);
        Button getAnswerButton = (Button) findViewById(R.id.button1);

        getAnswerButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String[] asnwers = {
                        "It is certain",
                        "It is decidedly so",
                        "All signs say yes",
                        "The stars are not aligned",
                        "my reply is no",
                        "It is doubtful",
                        "Better not tell you now",
                        "Concentrate and ask again",
                        "Unable to answer now",
                        "It is hard to say"};

                // The button was clicked, so update the answer label with an answer
                String answer = "";

                //Randomly select one of three answers: Yes, No or Maybe
                Random randomGenerator =  new Random(); // Construct a new Random number generator
                Object answers;
                int randomNumber = randomGenerator.nextInt(answers.length);

                /* Convert the randomNumber to a text answer
                 *  0 - Yes
                 *  1 - No
                 *  2 - Maybe
                 */

                answer = answers[randomNumber];

                //Update the label with our dynamic answer
                answerLabel.setText(answer);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

[ed. note] Added markdown to fix code formatting

4 Answers

This is for the android crystal ball app by the way. Any help would be great. As far as I know, all the code has been followed exactly as was instructed.

Russell - I haven't done the Android course yet, so I won't but much help. However, here's a suggestion from, your friendly neighborhood mod :tm: ...

You might get better responses to your query if, you take a look our featured post on how to ask good questions on the forum

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

This is the kind of error that makes you want to facepalm--check your spelling when you declare your "answers" String array. :smile:

Oh, wow. I'm going to hang my head in shame now...