-2

I am working on a course project and I ran into an issue while making the program in netbeans. I coded the program in blueJ and everything worked fine there but whenever I moved everything to netbeans and I ran into a few issues. Here is the main method: Note: There was some other code there, but it does not show up in the netbeans so I just left it out. But it is still in java file.

/** 
 * @param args the command line arguments
 */
public static void main(String args[]) throws IOException {

    StudentInfo myStudents = new StudentInfo();
    myStudents.open("students.dat");

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);

        }
    });
}

Here is where I get the error:

myStudent.writeStudent(myStudents);

says it cannot find the symbol myStudents.

Finally here is the writeStudents

public void writeStudent(Student inS) throws IOException
{
    int n = inS.getStudentID() - 901000000;
    students.seek(n * RECORD_SIZE);
    students.writeInt(inS.getStudentID());
    String lastName = padString(inS.getLastName());

    for (int i=0; i < STRING_SIZE; i++)
        students.writeChar(lastName.charAt(i));

    String firstName = padString(inS.getFirstName());

    for (int i=0; i<STRING_SIZE; i++)
        students.writeChar(firstName.charAt(i));

    String address = padString(inS.getAddress());

    for(int i=0; i<STRING_SIZE; i++)
        students.writeChar(address.charAt(i));

    students.writeDouble(inS.getWageRate());
    students.writeDouble(inS.getHoursWorked());
}
Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58

1 Answers1

0

myStudents is a local variable inside of main() that means you can't use it anywhere else in your program except in the main function unless there is another variable with the same name. Also note that myStudents if of type StudentInfo but writeStudent requires an argument of type Student.

WillShackleford
  • 6,918
  • 2
  • 17
  • 33
  • Thanks for the info, I figured that out today after I started working a little bit harder on it. – Jacob Staggs Nov 18 '15 at 20:47
  • Sorry for asking another question on here. But, I ran into the Error that the non-static variable cannot be referenced from a static context. But, I seen some posts that if I did this then it would work. public static void main (String args[]) throws IOException{ java.awt.EventQueue.invokeLater(new Runnable() { public void run(){ new NewJFrame().setVisible(true); myStudents.open("students.dat"); } – Jacob Staggs Nov 18 '15 at 21:00
  • static functions like main can only use variables declared inside of them or static variables. If you moved the definition of myStudents outside of the main function but still use it inside main you will need to declare myStudents to be static. – WillShackleford Nov 18 '15 at 21:37