Hello.
I'm having a problem with a program i'm writing at the moment.
The program should store information about students(name, surname, module, grades etc.)
So i created a class of type Student that will store all this information, and another class SystemM that contains methods for adding students, removing, viewing details and loading and saving to file.
But when i create a new student in my GUI class and send it to the System class i can't access this object.
Let me post my code.
package modulegradebook; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JOptionPane; /** * * @author Kris_the_Savage */ public class SystemM { private PrintStream outFile; private FileReader inFile; private ArrayList<Student> student = new ArrayList<Student>(); private ArrayList grade = new ArrayList(); //private ArrayList<User> user; SystemM(){} //***************************************************************************** //this method will save created students to file // //@param none // //return none //***************************************************************************** public void saveStudentToFile(){ try { System.out.print("save"); outFile = new PrintStream(new File("Students.txt")); for (int i = 0; i < student.size(); i++) { // outFile.write(i); //students id outFile.println(student.get(i).getName()); outFile.println(student.get(i).getSurname()); outFile.println(student.get(i).getAddres()); outFile.println(student.get(i).getModuleName()); outFile.println(student.get(i).getGradesArray()); } } catch (IOException ex) { System.err.print("Can't open file"); } }//end of saveStudent method //***************************************************************************** //this method will load students from file // //@param filename // //return ArrayList of type Student wit all students created //***************************************************************************** public void loadStudentFromFile(String filename){ try { Scanner inFile = new Scanner(new FileReader(filename)); while (inFile.hasNext()) { String name = inFile.nextLine(); String surname = inFile.nextLine(); String addres = inFile.nextLine(); String module = inFile.nextLine(); while(inFile.hasNextInt()){ int inGrade = inFile.nextInt(); grade.add(inGrade); } Student s = new Student(name, surname, module, addres); student.add(s); //add created student to arraylist s.setGrades(grade); for(int x = 0;x<student.size(); x++){ System.out.println("load " + student.get(x).getName()+student.get(x).getSurname()+student.get(x).getGradesString()); } } //return student; } catch (FileNotFoundException ex) { } //return student; }//end of loadStudentFromFile method //***************************************************************************** //this method displays details of chosen student //called by academics // //@param none // //return none //***************************************************************************** public void viewStudentDetails(){ String vName = JOptionPane.showInputDialog("Enter students name"); for(int v = 0; v<student.size(); v++){ System.out.println(student.get(v).getName() + "\n" + student.get(v).getSurname() + "\n" + student.get(v).getAddres() + "\n" + student.get(v).getModuleName()); //if entered name matches concatenated name and surname of student //then display that students details vName.trim(); //remove space if(vName.equalsIgnoreCase(student.get(v).getName().concat(" ").concat(student.get(v).getSurname()))){ JOptionPane.showMessageDialog(null, student.get(v).getName() + "\n" + student.get(v).getSurname() + "\n" + student.get(v).getAddres() + "\n" + student.get(v).getModuleName()); } } }//end of viewStudentDetails //***************************************************************************** //this method will create a student and add it to the arraylist //It will be called by admin and only by admin // //@param none // //return none //***************************************************************************** public void addStudent(){ DetailsGUI d = new DetailsGUI(); }//end of addd student method //***************************************************************************** //this method deletes chosen student from list //called by and only by admin // //@param none // //return none //***************************************************************************** public void deleteStudent(){ String delName = JOptionPane.showInputDialog("Enter name of the student you wish to delete"); for(int d = 0; d<student.size(); d++){ //if entered name matches concatenated name and surname of student //then delete that student delName.trim(); //remove space if(delName.equalsIgnoreCase(student.get(d).getName().concat(" ").concat(student.get(d).getSurname()))){ student.remove(d); } } }//end of deleteStudent method //***************************************************************************** //this method displays marks of all students with their names and surnames //can be called by admin and academic // //@param none // //return none //***************************************************************************** public void viewAllStudentsAndMarks(){ System.out.println("its here"); //just for debbuging for(int i = 0;i<student.size(); i++){ JOptionPane.showMessageDialog(null, student.get(i).getName() + " " + student.get(i).getSurname() + " " + student.get(i).getGradesString() + "\n"); } }//end of viewAllStudentsAndMarks method //***************************************************************************** //Getter methodes //***************************************************************************** public ArrayList<Student> getStudentList(){ return student; } //***************************************************************************** //Setter methodes //***************************************************************************** public void setStudent(Student st){ student.add(st); //for debbuging for(int i=0;i<student.size();i++){ System.out.print(student.get(i).getName() + " " + student.get(i).getSurname() + " " + student.get(i).getModuleName() + "\n");} } }
This is my SystemM class. When i use the setStudent method i see that student was passed into this class but i can't acces it with any other method. And it seems that adding next object of type Student overwrites the existing one. When next i try call method public void viewAllStudentsAndMarks() it does not enter the loop for which suggests that ArrayList does not exist.
I would really appreciate help. I'm quite new to programming so if i'm making some stupid mistake please be gentle
Thanks in advance.