Ok i just changed my code to use true in the while loop. Now when it is reading from the file i get a stream corrupted exception. cold this be because of the obin not being new.
private class viewStudentListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
ArrayList<Student> students = new ArrayList<Student>();
FileInputStream fin = new FileInputStream("Students.bin");
ObjectInputStream obin = new ObjectInputStream(fin);
int id = Integer.parseInt(ID.getText());
int count = 0;
while(true)
{
try{
Student s = (Student) obin.readObject();
students.add(s);
}
catch(EOFException eofException){
break;
}
}
while (count < students.size())
{
if(students.get(count).equals(id))
{
fName.setText(students.get(count).getFirstName());
lName.setText(students.get(count).getLastName());
}
count++;
}
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
} }
This is how the file is being written
private class createNewStudentListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Student s = new Student(enterFirstName.getText(), enterLastName.getText());
File newStudent;
newStudent = new File("Students.bin");
FileOutputStream outStream = new FileOutputStream("Students.bin", newStudent.exists());
ObjectOutputStream objectOutputFile = new ObjectOutputStream(outStream);
objectOutputFile.writeObject(s);
JOptionPane.showMessageDialog(null, "The new student was added successfully");
String id = Integer.toString(s.getID());
IDNUM.setText(id);
s = null;
objectOutputFile.close();
}
catch (emptyString a)
{
JOptionPane.showMessageDialog(null, a.getMessage());
} catch (Exception a) {
JOptionPane.showMessageDialog(null, a.getMessage());
}
}
}
}