不断获取java.io.NotSerializableException

时间:2018-09-29 21:20:48

标签: java serialization

我有一个名为Parameters的类,该类实现了Serializable。但是,当我使用writeObject(p)时,出现了错误消息。我检查了p的状态。它说它已经被序列化了。

package Server.Interface;
import java.util.*;
import java.io.Serializable;

public class Parameters implements Serializable{
    private String command;
    private String location;
    private ArrayList<Integer> numbers;
    public Parameters(String command, String location,ArrayList<Integer> numbers) {
        this.command = command;
        this.location = location;
        this.numbers = numbers;
}

    public String getCommend() {
        return command;
    }

    public void setCommend(String c) {
        this.command = c;
    }

    public String getLocation() {
        return location;
    }

    public void setNumbers(ArrayList<Integer> numbers) {
        this.numbers = numbers;
    }
}

客户代码:

public void sendMessage(Parameters p) {
    try {
        if(p instanceof Serializable){
            System.out.println("can be serialized");
        }
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(p);
        InputStream is=socket.getInputStream();
        int attempts = 0;
        while(is.available() == 0 && attempts < 1000)
        {
             System.out.println(attempts);
             attempts++;
             try {
                 Thread.sleep(10);
             }
             catch (InterruptedException e) {
                    e.printStackTrace();
             }
        }
        System.out.println("============================");
        BufferedReader br=new BufferedReader(new InputStreamReader(is));
        System.out.println("============================");
        String info=null;
        while((info=br.readLine())!=null&&info.length()>0){
            System.out.println(info);
            break;
        }
    }
    catch (IOException e) {
        System.out.println("IOError");
        e.printStackTrace();
    }
}

这是消息:

can be serialized
IOError
java.io.NotSerializableException: Client.TCPClient

它清楚地表明该对象已序列化。但是writeObject(p)失败。我也尝试将p的arraylist设置为null,并通过了。但是ArrayList默认情况下可以序列化吗?它不应该影响序列化。

0 个答案:

没有答案