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 (retired 2014) Learning the Language Simple Refactoring

crystalball cannot be resolved to a type

I get the error message "crystalball cannot be resolved to a type"

Help?

public class MainActivity extends Activity {

    private Crystalball mCrystalball = new CrystalBall();

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

         // Declare our view variables
         final TextView answerLabel = (TextView) findViewById(R.id.textView1);
         Button getAnswerButton = (Button) findViewById(R.id.button1);

         getAnswerButton.setOnClickListener(new View.OnClickListener() {


            public void onClick(View arg0) {
                String answer = mCrystalBall.getAnAnswer();

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

6 Answers

Gergő Bogdán
Gergő Bogdán
6,664 Points

You should write types in CamelCase, the problem is you wrote Crystalball (small b in ball) and the type is CrystalBall (Ball with capital b)

I tested your solution but I get the same error...

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Ivan Bodnar, can you paste in your code from CrystalBall.java?

Sure:

package com.example.crystalball;

import java.util.Random;

public class crystalBall {
    // Member variables (properties about the object)

    // Methods (abilities: things the object can do)
    public String getAnAnswer() {
        String[] answers = {
                "It is certain",
                "It is decidedly so",
                "All sings YES",
                "The stars are not aligned",
                "My reply is NO",
                "It is doubtful",
                "Better not tell you know",
                "Concentrate and ask again",
                "Unable to answer now",

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



        //Random select one of three answers: yes, no or maybe
        Random randomGenerator = new Random(); // Construct a new Random number generator
        int randomNumber = randomGenerator.nextInt(answers.length);

        /* Convert the random number to a text answer
         * 0 = Yes
         * 1 = No
         * 2 = Maybe
         */

        answer = answers[randomNumber];

        return answer;
    }

}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Ah, check out your class name. It has a lowercase "c". You either need to change it to an uppercase "c" or use the lowercase version everywhere. Java convention is to use uppercase for class names, so I'd go that route. :)

Gergő Bogdán
Gergő Bogdán
6,664 Points

Now its clear what is the issue. In Java the name of the class has to be exactly the same as the name of the file which contains it. So in your case if the .java file the CrystalBall class is is implemented is named as <b>CrystalBall</b> then the name of the class inside the code file has to be the exactly the same, which in your case is not (crystalBall). Please try to write in the class declaration the exact name which you have as file name. That should resolve your issue.

I guess I missed the beginning:

package com.example.crystalball;

import java.util.Random;

public class crystalBall { // Member variables (properties about the object)

// Methods (abilities: things the object can do)
public String getAnAnswer() {
    String[] answers = {

Okkkk!!! Now it's working! Thank you both guys.

Ps: sorry I kind of broke your Crystals ;)