Note that there are some explanatory texts on larger screens.

plurals
  1. POJava writeObject is writing an empty ArrayList
    primarykey
    data
    text
    <p>I'm having trouble writing an <code>ArrayList</code> of a custom class to a file. I'm using</p> <pre><code>public static void SaveEmployees() throws FileNotFoundException, IOException{ ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("magicscheduleEmp.sav"); save.writeObject(employeeList); save.close(); } </code></pre> <p>Where <code>employeeList</code> is declared and initialized as an <code>ArrayList</code> of <code>Employees</code> (my class) in the main method. I have definitely added objects to <code>employeeList</code>, but calling <code>SaveEmployees</code> writes an empty <code>ArrayList</code> to the file. The <code>Employee</code> class is serializable.</p> <p>What am I doing wrong?</p> <p>Edit: employeeList is initialized here (in the main method)</p> <pre><code> public static ArrayList&lt;Employee&gt; employeeList = new ArrayList&lt;&gt;(); </code></pre> <p>and populated through user input (in another jFrame class)</p> <pre><code> if(MainUI.addempcheck == true){ Employee newEmployee = new Employee(LastName.getText(), FirstName.getText()); newEmployee.position = EmpType.getSelectedItem().toString(); MainUI.employeeList.add(newEmployee); MainUI.listModel.addElement(newEmployee.writeName().toString()); } try { MainUI.SaveEmployees(); } catch (FileNotFoundException ex) { Logger.getLogger(NewEmployeeUI.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(NewEmployeeUI.class.getName()).log(Level.SEVERE, null, ex); } this.dispose(); </code></pre> <p>I have a button which prints the contents of employeeList, and shows that the list is populated before I save it. Maybe it's a problem with calling SaveEmployees?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload