Results 1 to 5 of 5
  1. #1
    seral1969 is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Angry Cannot Resolve Symbol error...

    Hi, I'm new to Java...

    I've been trying to get one of my professor's examples to work...

    He says that the code is fine, but I keep getting 4 "cannot resolve symbol" errors when I try to compile the ShowStudent.java file...

    It seems that ShowStudent cannot access the Student class...

    They are in the same directory...

    Using XP...

    Command line compiler is javac...

    The code for the Student class and ShowStudent program are listed below...

    PLEASE HELP!!!

    --------------------------------------------------------------------------

    // Student.java
    // creates a class to store info about a student

    class Student
    {
    // the private data members
    private int IDnumber;
    private int hours;
    private int points;

    // constructor added in last part of project
    Student()
    {
    IDnumber = 77375;
    points = 12;
    hours = 3;
    }
    // end of constructor

    // the public get and set methods
    public void setIDnumber(int number)
    {
    IDnumber = number;
    }


    public int getPoints()
    {
    return points;
    }

    // methods to display the fields
    public void showIDnumber()
    {
    System.out.println("ID Number is: " + IDnumber);
    }

    public void showHours()
    {
    System.out.println("Credit Hours: " + hours);
    }

    public void showPoints()
    {
    System.out.println("Points Earned: " + points);
    }

    public double getGradePoint()
    {
    return (double)points / hours;
    }

    }

    --------------------------------------------------------------------------

    // ShowStudent.java
    // client to test the Student class

    class ShowStudent
    {
    public static void main (String args[])
    {
    Student pupil = new Student();


    pupil.showIDnumber();
    pupil.showPoints();
    pupil.showHours();
    System.out.println("The grade point average of the studnet created by constructor is "
    + pupil.getGradePoint()+"\n\n");

    Student s2 = new Student();
    s2.setIDnumber(12345);
    s2.setPoints(66);
    s2.setHours(20);
    s2.showIDnumber();
    s2.showPoints();
    s2.showHours();
    System.out.println("The grade point average of another student is "
    + s2.getGradePoint()+"\n");

    }
    }

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    I'm not too sure about this ,but it might be because the classes are package access. This is because, in your class declaration statements, you didn't include an access modifier, resulting in "package" as being the default. What i mean is, you put "class Student" instead of "public class Student" and when you don't specify, it results in "package class Student". So, they might not be in the same package. This might not be the case here though, so yeah.

  3. #3
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    I forgot to mention, in case you didn't know, when something is declared with package access, that thing can only be accessed by classes that are within the same package.

  4. #4
    seral1969 is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Default Tried that...

    I've tried the pubic class... Still the same errors...

    Below is the result of what happens when I compile the files...


    02/17/2008 01:13 AM <DIR> .
    02/17/2008 01:13 AM <DIR> ..
    02/17/2008 01:13 AM 754 ShowStudent.java
    02/16/2008 06:20 PM 885 Student.java
    2 File(s) 1,639 bytes
    3 Dir(s) 109,830,705,152 bytes free

    C:\Error>javac Student.java

    C:\Error>java Student
    Exception in thread "main" java.lang.NoClassDefFoundError: Student
    Caused by: java.lang.ClassNotFoundException: Student
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

    C:\Error>javac ShowStudent.java
    ShowStudent.java:8: cannot find symbol
    symbol : class Student
    location: class ShowStudent
    Student pupil = new Student();// 2 cannot resolve sybmol... points to 'S' in Student
    ^
    ShowStudent.java:8: cannot find symbol
    symbol : class Student
    location: class ShowStudent
    Student pupil = new Student();// 2 cannot resolve sybmol... points to 'S' in Student
    ^
    ShowStudent.java:17: cannot find symbol
    symbol : class Student
    location: class ShowStudent
    Student s2 = new Student();// 2 cannot resolve sybmol points to 'S in Student
    ^
    ShowStudent.java:17: cannot find symbol
    symbol : class Student
    location: class ShowStudent
    Student s2 = new Student();// 2 cannot resolve sybmol points to 'S in Student
    ^
    4 errors

    C:\Error>java ShowStudent
    Exception in thread "main" java.lang.NoClassDefFoundError: ShowStudent
    Caused by: java.lang.ClassNotFoundException: ShowStudent
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

    C:\Error>

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Well this is certainly odd.. When I examined it, it didn't find 4 unresolved symbols, but it did find two. See the code below for the two lines I commented out- you don't have methods for what you're trying to call(setPoints, and setHours).

    Please use code tags when posting code.
    Java Code:
    // Student.java
    // creates a class to store info about a student
    
    class Student {
      // the private data members
      private int IDnumber;
      private int hours;
      private int points;
    
      // constructor added in last part of project
      Student() {
        IDnumber = 77375;
        points = 12;
        hours = 3;
      }
      // end of constructor
    
      // the public get and set methods
      public void setIDnumber(int number) {
        IDnumber = number;
      }
    
    
      public int getPoints() {
        return points;
      }
    
      // methods to display the fields
      public void showIDnumber() {
        System.out.println("ID Number is: " + IDnumber);
      }
    
      public void showHours() {
        System.out.println("Credit Hours: " + hours);
      }
    
      public void showPoints() {
        System.out.println("Points Earned: " + points);
      }
    
      public double getGradePoint() {
        return (double)points / hours;
      }
    }
    Java Code:
    // ShowStudent.java
    // client to test the Student class
    
    class ShowStudent {
      public static void main (String args[]) {
        Student pupil = new Student();
        pupil.showIDnumber();
        pupil.showPoints();
        pupil.showHours();
        System.out.println("The grade point average of the studnet created by constructor is "
                              + pupil.getGradePoint()+"\n\n");
    
        Student s2 = new Student();
        s2.setIDnumber(12345);
    [B]//    s2.setPoints(66);
    //    s2.setHours(20);[/B]
        s2.showIDnumber();
        s2.showPoints();
        s2.showHours();
        System.out.println("The grade point average of another student is "
              + s2.getGradePoint()+"\n");
      }
    }
    Resulting output:
    ID Number is: 77375
    Points Earned: 12
    Credit Hours: 3
    The grade point average of the studnet created by constructor is 4.0


    ID Number is: 12345
    Points Earned: 12
    Credit Hours: 3
    The grade point average of another student is 4.0

    Evidently, the code I was just using was able to get access... You may want to check your PATH & CLASSPATH variables. Otherwise, try the code above. I'm not using your system so I can't tell you what's wrong unless you provide more system info.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  2. Replies: 1
    Last Post: 12-21-2007, 12:12 PM
  3. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:39 AM
  4. Error: cannot resolve symbol, help me
    By mathias in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-06-2007, 03:46 PM
  5. MySite/Valid.java:56: cannot resolve symbol
    By Albert in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-05-2007, 06:49 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
  •