Results 1 to 8 of 8
  1. #1
    Acadian is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Object cannot find symbol. Solved

    I'm new to Java and I've been going through the book Head-First-Java 2nd edition. I'm learning about objects and how to call them from within a different class, I follow the book exactly, but I'm getting a cannot find symbol error.

    Here is my code.

    Java Code:
    class Movie {
    String title;
    String genre;
    int rating;
    void playIt() {
    System.out.println("Playing the movie");
    	}
    }
    Java Code:
    public class MovieTestDrive {
    public static void main(String[] args) {
    	Movie one = new Movie();
    	one.title = "Gone with the Stock";
    	one.genre = "Tragic";
    	one.rating = -2;
    	Movie two = new Movie();
    	two.title = "Lost in Cubicle Space";
    	two.genre = "Comedy";
    	two.rating = 5;
    	two.playIt();
    	Movie three = new Movie();
    	three.title = "Byte Club";
    	three.genre = "Tragic but ultimately uplifting";
    	three.rating = 127;
    	}
    }
    I can compile Movie.java properly, but when I try to compile MovieTestDrive.java, I get this output.

    ..\sample\test\MovieTestDrive.java:3: cannot find symbol
    Movie one = new Movie();
    ^
    symbol: class Movie
    location: class MovieTestDrive
    ..\sample\test\MovieTestDrive.java:3: cannot find symbol
    Movie one = new Movie();
    ^
    symbol: class Movie
    location: class MovieTestDrive
    ..\sample\test\MovieTestDrive.java:7: cannot find symbol
    Movie two = new Movie();
    ^
    symbol: class Movie
    location: class MovieTestDrive
    ..\sample\test\MovieTestDrive.java:7: cannot find symbol
    Movie two = new Movie();
    ^
    symbol: class Movie
    location: class MovieTestDrive
    ..\sample\test\MovieTestDrive.java:12: cannot find symbol
    Movie three = new Movie();
    ^
    symbol: class Movie
    location: class MovieTestDrive
    ..\sample\test\MovieTestDrive.java:12: cannot find symbol
    Movie three = new Movie();
    ^
    symbol: class Movie
    location: class MovieTestDrive
    6 errors


    Thanks
    Last edited by Acadian; 03-02-2011 at 11:18 PM. Reason: Solved

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Are the two .java files in the same directory? If not you will need to use an import statement or simply place them in the same folder.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As junky stated, put them in the same directory and your code should work. By the way, head first java does a great job covering stuff. If you need any help, dont hesitAte to come ask questions here, also, feel free to send memmsges if needed for help.
    Last edited by sunde887; 03-02-2011 at 10:40 PM.

  4. #4
    Acadian is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Yes they are both in a directory in my jdk root called sample\test I am invoking javac from the bin directory on the command line like this.

    javac ../sample/test/Movie.java then when that compiles ok

    javac ../sample/test/MovieTestDrive.java

    in a command prompt.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It might be easier to save them in a new directory and then cd to the directory and then compile like that, try cding to the directory containing them in the root directory you saved them in and try compiling like that

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    That won't work as it is trying to find the Movie class in the current working directory ie the bin directory. A better approach is to modify your environment variable so it know where the java and javac exe's are. Then you cd to the directory where your files are to compile and run.

  7. #7
    Acadian is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks, that was the problem :)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Don't do your work in the JDK directory.
    It's a bad habit to get into.

Similar Threads

  1. Cannot find symbol
    By Johanis in forum New To Java
    Replies: 18
    Last Post: 11-09-2010, 08:34 PM
  2. Still cannot find symbol!
    By Johanis in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 04:32 PM
  3. Can not find symbol ???
    By AliceNewbie in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 01:44 AM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •