Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting to a file from an array list and Vice Versa
    primarykey
    data
    text
    <p>I have created a small program with 3 buttons and 1 text field for user input. What the program does is, it takes input from the user and stores it to an arraylist, then when the second button is pressed it should write the arraylist in to the file. When the third button is pressed it should read from the file in to an arraylist. </p> <p>My problem is am getting only the first record , meaning if i enter 2 names , i only get the first name displayed when i press the third button. Here is what i have done:</p> <p>2 arraylists are decalred.</p> <pre><code> ArrayList&lt;Person&gt;list=new ArrayList&lt;Person&gt;() ArrayList&lt;Person&gt;list2=new ArrayList&lt;Person&gt;(); </code></pre> <p>The button which takes user input when pressed</p> <pre><code> String firstname=txtname.getText(); Person p1=new Person(firstname,"pamodya","15","08","1995"); list.add(p1); </code></pre> <p>The button which writes the first arraylist object in to the file. try{</p> <pre><code> FileOutputStream write=new FileOutputStream("hello.txt"); ObjectOutputStream writeFile=new ObjectOutputStream(write); writeFile.writeObject(list); writeFile.flush(); writeFile.close(); }catch(Exception e){e.getMessage();} </code></pre> <p>The button which reads from the file and prints to the text area when pressed.</p> <pre><code> try{ FileInputStream read=new FileInputStream("hello.txt"); ObjectInputStream readFile=new ObjectInputStream(read); Person p1=(Person)readFile.readObject(); list2.add(p1); readFile.close(); txtarea.setText(p1.getName()); }catch(Exception e){e.getMessage();} </code></pre> <p>What seems to be the problem here? Thank you for your time.</p> <p>EDIT :</p> <p>This is the error </p> <p><img src="https://i.stack.imgur.com/tOFy5.png" alt="enter image description here"></p>
    singulars
    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.
    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