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

Java Java Objects (Retired) Meet Objects Creating New Objects

Eleni Minadaki
Eleni Minadaki
3,687 Points

error:cannot find symbol

Hi!i try to find what i'm doing wrong but i can't,in the question : print out using the color of the new object,using the getColor method. The error appears is:can't find symbol and the warning is at c from first word color. Here is my code: public class Example { public static void main(String[] args) { System.out.println("We are going to create a GoKart"); GoKart goKartObject = new GoKart("red"); System.out.printf("New GoKart is %s\n", color.getColor()); } }

Example.java
public class Example {

    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
      GoKart goKartObject = new GoKart("red"); 
      System.out.printf("New GoKart is %s\n", color.getColor());
    }
}

4 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

color.getColor() is where the problem is. Because you never created an object named color, your GoKart object's name is goKartObject, so change it to goKartObject.getColor() instead.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Eleni;

Welcome to Treehouse!

As William pointed out, you were making reference to an undefined object, and you got your mistake taken care of. That's great. If you don't mind I would like to expand a bit on the error in general just to help prevent it (or minimize it) in the future. I say that, but they still happen to the best programmers out there from time to time. Using a language specific (Java) development environment such as IntelliJ or Eclipse helps to reduce the errors at run time, but let's take a look at them.

To start with when a Java program is compiled, the compiler looks at various things and it can either identify them or not. Things such as:

  • Keywords: class, private, for, etc.
  • Operators and tokens: [, <, *, etc.
  • Comments and whitespace
  • Literals: 121, X, "Hello World!", etc.
  • Identifiers: j, toString, GoKart, saveMemeToDatabase, etc.

As the compiler is going through the code it will find something and know what to do with it or not. Your Cannot find symbol error relates to the identifiers and means that Java cannot figure out what the "symbol" means. What then causes a "Cannot find symbol" error?

Basically as the compiler is doing it's job, you are asking it to do something with a symbol it cannot define. Think of taking a test in grammar school in which you have to define words. Cat: a furry little feline house pet. Car: an enclosed motorized form of transportation. Kdjlemzqr: Sorry, I don't know. Java is similar to that and gives an error.

The general causes for a Cannot find symbol error are things like:

  • Incorrect spelling. Java doesn't attempt to understand that when you write GoKrat you really meant GoKart.
  • Wrong case. goKart is different from GoKart.
  • Improper use of acceptable identifier values (letters, numbers, underscore, dollar sign), this_is_my_variable is not the same as thisismyvariable.
  • No variable declaration or variable is outside of the scope you are referencing it in.

Those are some of the more common ones, there are others dealing with method and class names, and you can get into combinations of errors for debugging as well when importing packages and classes.

One thing to keep in mind as well as you are starting out is that it is possible to redefine common library classes. In the beginning of the learning process it can be problematic because we don't know what they all are. For example, you could create your own String class and then attempt to use common methods on that and you will get an error.

I hope that helps a bit.

Happy coding,

Ken

Eleni Minadaki
Eleni Minadaki
3,687 Points

It works! Thanks a lot William,appreciate your help!

Eleni Minadaki
Eleni Minadaki
3,687 Points

Hello Ken, i am new in Java and such analytic answer as yours is very helpful for me! Thanks a lot for your time,appreciate!!