1

I'm quite new to java and unsure how to fix this java.io.NotSerializableException error.

I'm trying to use an add button on the GUI to add an object to an array list Then write that object to a file so I am able to read it back.

Here is the code I'm using for the Branch class which implement Java Serializable:

import java.io.Serializable;

public class Branch implements Serializable{
    private String branch_name;
    private String branch_address;

    public Branch(String Bname, String Baddress) {
        this.branch_name = Bname;
        this.branch_address = Baddress;

        public String getbranch_name(){
            return branch_name;
        }

        public String getbranch_address(){
            return branch_address;
        }

        public void show_branch_details() {
            System.out.println( " The branch name is : " + getbranch_name()
                    + " branch address :"+ getbranch_address()
        }
    }
}

Here is the code for the add button:

ArrayList<Branch> BranchList = new ArrayList<Branch>();
JButton AddBranch = new JButton("ADD BRANCH");
AddBranch.setBounds(10, 35, 161, 23);
AddBranch.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String Bname = branchNameField.getText();
        String Baddress = branchAddressField.getText();

        Branch A = new Branch(Bname, Baddress);

        BranchList.add(A);

        for (int i = 0; i < BranchList.size(); i++) {
            displayInfo.append(BranchList.get(i).getbranch_name() +);
        }
        System.out.println("The ArrayList has " + BranchList.size());
        for (int i = 0; i < BranchList.size(); i++) {
            System.out.println(BranchList.get(i).getbranch_name());
        }
        try {
            FileOutputStream fos = new FileOutputStream("branch.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            //oos.writeObject(BranchList);
            for (int b = 0; b < BranchList.size(); b++) {
                oos.writeObject(BranchList.get(b));
            }
            oos.flush();
            oos.close();

            FileInputStream fis = new FileInputStream("branch.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            //BranchList = (ArrayList<Branch>)ois.readObject();
            Branch obj = null;

            while ((obj = (Branch) ois.readObject()) != null) {
                System.out.println("Name:" + obj.getbranch_name() + ", Address:"
                        + obj.getbranch_address());
            }
            ois.close();
        } catch (IOException ex) {
            System.out.println(" IOE ERROR");
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            System.out.println("class ERROR");
            ex.printStackTrace();
        }
    }
});
jose praveen
  • 1,298
  • 2
  • 10
  • 17
Shan
  • 11
  • 5
  • 3
    Could you show us the output of the program and the complete stack trace? – omajid May 28 '20 at 17:50
  • Is this what you mean The ArrayList has 1 Testing IOE ERROR java.io.NotSerializableException: Branch at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185) – Shan May 28 '20 at 17:54
  • 1
    Can you try adding this line in your Branch class ? private static final long serialVersionUID = 4L; – Harmandeep Singh Kalsi May 28 '20 at 17:55
  • Added that and unfortunately im still getting the same error – Shan May 28 '20 at 17:57
  • Rather than writing it to file in loop , can you simply do FileOutputStream fos = new FileOutputStream("branch.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(BranchList); oos.close(); fos.close(); – Harmandeep Singh Kalsi May 28 '20 at 18:02
  • Please include your `Branch` class... the thing you posted doesn't compile. – Šimon Kocúrek May 28 '20 at 18:02
  • Check this link for reference : https://howtodoinjava.com/java/collections/arraylist/serialize-deserialize-arraylist/ – Harmandeep Singh Kalsi May 28 '20 at 18:03
  • The Branch class is at the top – Shan May 28 '20 at 18:06
  • That does not compile.. so clearly it's different from what you are using. – Šimon Kocúrek May 28 '20 at 18:06
  • I tried the FileOutputStream fos = new FileOutputStream("branch.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(BranchList); oos.close(); fos.close(); before however it still didnt work which is why i then tried the loop instead. As you can see i commented that line out oos.writeObject(BranchList) as it didnt work in the beginning – Shan May 28 '20 at 18:08
  • I added the Branch class in answer . Please use that . – Harmandeep Singh Kalsi May 28 '20 at 18:10
  • ok could you also tell me what you changed so i know for future use please – Shan May 28 '20 at 18:12
  • Did it work for you ? You included everything in the constructor. The closing brackets were missing – Harmandeep Singh Kalsi May 28 '20 at 18:14
  • No unfortunately im stil getting the same error. Im really at a loss as to what the problem is – Shan May 28 '20 at 18:16
  • Is there anything else i can try? – Shan May 28 '20 at 18:27
  • I added a running example . Please check and change according to your JButton event listener. This code in answer works fine – Harmandeep Singh Kalsi May 28 '20 at 18:35
  • The issue is that im supposed to add to the array list through taking the input from the jtextfields and then putting them in an arrayList. The example you gave does it manually through BranchList.add(A); BranchList.add(B); so the code im writing is in the action performed method for the add branch button, not the main method. would you know how to make adjustments to it to fit those requirements? – Shan May 28 '20 at 18:41
  • So basically i have a gui that takes the branch name and address. It is then supposed to create an object in the array list, write it to the file so that it can be read if needed all when the add branch button is selected – Shan May 28 '20 at 18:45

1 Answers1

0

Please use this as Branch class :

        import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

    class Branch implements Serializable{

        private String branch_name;
        private String branch_address;

        public Branch(String Bname, String Baddress) {
            this.branch_name = Bname;
            this.branch_address = Baddress;
        }
            public String getbranch_name(){
                return branch_name;
            }

            public String getbranch_address(){
                return branch_address;
            }

            public void show_branch_details() {
                System.out.println( " The branch name is : " + getbranch_name()
                        + " branch address :"+ getbranch_address());
            }

        }

    public class Test {

        public static void main(String[] args) throws IOException, ClassNotFoundException {

            Branch A= new Branch("TestA","Add_A");
            Branch B= new Branch("TestB","Add_B");

            ArrayList<Branch> BranchList = new ArrayList<>();
            BranchList.add(A);
            BranchList.add(B);

            FileOutputStream fos = new FileOutputStream("branch.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(BranchList);
            oos.flush();
            oos.close();

            ArrayList<Branch> OutputBranchList = new ArrayList<>();
            FileInputStream fis = new FileInputStream("branch.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            OutputBranchList = (ArrayList) ois.readObject();

            for(Branch branch : OutputBranchList) {
                System.out.println(branch.getbranch_name()+" "+ branch.getbranch_address());
            }
            ois.close();
        }
    }
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26