【问题标题】:How can i fix this java.io.NotSerializableException: error in java?我该如何解决这个 java.io.NotSerializableException: java 中的错误?
【发布时间】:2020-05-28 17:38:19
【问题描述】:

我对 java 还很陌生,不确定如何修复这个 java.io.NotSerializableException 错误。

我正在尝试使用 GUI 上的添加按钮将对象添加到数组列表 然后将该对象写入文件,以便我能够将其读回。

这是我用于实现 Java SerializableBranch 类的代码:

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()
        }
    }
}

添加按钮的代码如下:

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();
        }
    }
});

【问题讨论】:

  • 你能告诉我们程序的输出和完整的堆栈跟踪吗?
  • 这就是你的意思吗?ArrayList 有 1 个测试 IOE 错误 java.io.NotSerializableException: Branch at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
  • 您可以尝试在您的 Branch 类中添加这一行吗? private static final long serialVersionUID = 4L;
  • 添加了,不幸的是我仍然遇到同样的错误
  • 而不是将其写入循环文件,您可以简单地执行 FileOutputStream fos = new FileOutputStream("branch.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(BranchList); oos.close(); fos.close();

标签: java serialization notserializableexception


【解决方案1】:

请将此作为分支类:

        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();
        }
    }

【讨论】:

    猜你喜欢
    • 2016-07-21
    • 2016-09-09
    • 1970-01-01
    • 2015-02-28
    • 2013-08-16
    • 2020-04-17
    相关资源
    最近更新 更多