Tyler Robinson
Tyler Robinson

Reputation: 23

Error: Cannot Find Symbol

Okay, so I'm very new to Java, and I have no prior programming experience. I'm going through the Java tutorial, and everything is going alright until I run into a problem in the "Objects" section the tutorial.

The title of the program is Create Object Demo. The goal is to find the width, height, and area of one rectangle, and the new position of another. You do all of this using the premise of "Creating an object." The creating the object part is the problem.

Here's the original code:

public class CreateObjectDemo {

public static void main(String[] args) {

    // Declare and create a point object and two rectangle objects.
    Point originOne = new Point(23, 94);
    Rectangle rectOne = new Rectangle(originOne, 100, 200);
    Rectangle rectTwo = new Rectangle(50, 100);

    // display rectOne's width, height, and area
    System.out.println("Width of rectOne: " + rectOne.width);
    System.out.println("Height of rectOne: " + rectOne.height);
    System.out.println("Area of rectOne: " + rectOne.getArea());

    // set rectTwo's position
    rectTwo.origin = originOne;

    // display rectTwo's position
    System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
    System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);

    // move rectTwo and display its new position
    rectTwo.move(40, 72);
    System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
    System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
}
}

I run the program, and this is my error message:

CreateObjectDemo:.java:6: error: cannot find symbol
       Point originOne = new Point(23, 94);
       ^ 

symbol: class Point
location: class CreateObjectDemo
CreatObjectDemo.java:6: error: cannot find symbol
    Point originOne = new Point(23, 94);
                          ^

The full code and process is also located here

Likewise, the error message points to the word "Point" and "Rectangle" in the same fashion and claims that it "cannot find [the] symbol."

Any help would be greatly appreciated. I've been struggling with this error for a couple of days now. Thanks.

Upvotes: 2

Views: 8992

Answers (4)

Koo Boon Yao
Koo Boon Yao

Reputation: 1

The tutorial is not learning about GUI but just showing how to calculate the rectangle area using a method, so it is not necessary to import java.awt in the CreateObjectDemo.class. Just make sure that the Point and Rectangle classes are put in the same package with the CreateObjectDemo class, the error will surely disappear.

Upvotes: 0

umbregachoong
umbregachoong

Reputation: 349

For the CreateObject Demo there are two other classes in the tutorial you need to put in the same directory as the demo: the Point Class, and the Rectangle Class. They are described a couple of paragraphs down from the code of the demo.

So in netbeans and eclipse, put them in the same directory as CreateObjectDemo.java.

So in netbeans, go to File->New File->Java->Java Class->Class Name: Point etc

You do not need to import anything.

Hope this helps.

Upvotes: 0

William Morrison
William Morrison

Reputation: 11006

As Roddy suggests, you need to download and include the Point and Rectangle classes in the tutorial you are following. If you place those classes in the same directory as your CreateObjectDemo you'll not need import statements.

Exactly what's happening is the compiler is trying to convert your Java source code into byte code which the JVM (Java Virtual Machine) can interpret. For code to compile, all classes must be found. As the Point and Rectangle classes cannot be found, your code can't compile. Class Point and Rectangle are referred to as dependencies of class CreateObjectDemo for this reason. CreateObjectDemo won't work without them, it is dependent on Point and Rectangle.

Fixing this is easy, just make sure your CreateObjectDemo class knows where the Point and Rectangle classes are.

Upvotes: 1

neo108
neo108

Reputation: 5264

You will have to import the classes Point and Rectangle to your class. Add the following two lines on the top of your class, after the package line.

import java.awt.Point;
import java.awt.Rectangle;

If you are using Eclipse then just do CtrlShiftO, this will import the required classes for you.

Upvotes: 3

Related Questions