gpt4 book ai didi

java - ObjectInputStream 返回一个空对象

转载 作者:行者123 更新时间:2023-12-02 11:30:26 24 4
gpt4 key购买 nike

所以我试图将一个对象从客户端发送到服务器,然后我用这个对象做一些事情。我验证了发送时对象包含数据。当我在调试器中读取对象时,它的所有属性均为 null。

下面是我发送对象的位置:

private class registerUser extends Task<Void>
{
Users user;
private static final String code = ".register";
private static final String host = "localhost";
private static final int portNumber = 4444;
private clientThread clientThread;


public registerUser(Users user)
{
this.user = user;
}

@Override
protected Void call() throws Exception
{
try
{
Socket socket = new Socket(host, portNumber);
Thread.sleep(1000);

//Setup I/O
ObjectOutputStream outToServerObject = new ObjectOutputStream(socket.getOutputStream());
//PrintWriter serverOutString = new PrintWriter(socket.getOutputStream(), false);
InputStream serverInString = socket.getInputStream();

//serverOutString.println(code);
// serverOutString.flush();

outToServerObject.writeObject(user);
outToServerObject.flush();
}
catch (IOException e)
{
System.err.println("Fatal Connection error!");
e.printStackTrace();
}
return null;
}

当我按下 GUI 中的按钮时,就会使用上述任务,如下所示:

public void btn_registerUser(ActionEvent actionEvent) throws IOException
{

user.setUserName(txt_Username.getText());
user.setFirstName(txt_FirstName.getText());
user.setLastName(txt_LastName.getText());
user.setCity(txt_City.getText());
user.setBirthday(date_Birthday.getValue());


Task<Void> task = new registerUser(user);
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();

}

接收对象的服务器中的代码:

//Setup I/O
this.clientOut = new PrintWriter(socket.getOutputStream(), false);
ObjectInputStream inFromClientObject = new ObjectInputStream(socket.getInputStream());


// Scanner in = new Scanner(socket.getInputStream());
Users user = (Users) inFromClientObject.readObject();
doSomething(user);

还有 User.java 类,它是争论中的对象

public class Users implements Serializable
{

/*
Variables
TODO Create a friend list
*/
private transient IntegerProperty userID;
private transient StringProperty userName;
private transient StringProperty firstName;
private transient StringProperty lastName;
private transient StringProperty city;
private transient ObjectProperty<LocalDate> birthday;
private transient ListProperty<String> musicGenre;
private transient List<String> musicGenres = new ArrayList<>();
/**
* Default constructor.
*/
public Users()
{
this(null,null,null,null,null);
}

/**
* Constructor with some initial data.
* Takes in a {@code String} and sets a reference
*
* @param userName
*
*/
public Users(String userName, String firstName, String lastName, String city, LocalDate birthday)
{
this.userName = new SimpleStringProperty(userName);
this.userID = new SimpleIntegerProperty();
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.city = new SimpleStringProperty(city);
this.birthday = new SimpleObjectProperty<LocalDate>(birthday);
ObservableList<String> observableList = FXCollections.observableArrayList(this.musicGenres);
this.musicGenre = new SimpleListProperty<String>(observableList);

}
}

我省略了 getter 和 setter,因为这个问题已经很长了

因此,在调试器中运行服务器时,当我doSomething(user)

时,用户的所有属性均为空

*编辑这些建议正在发挥作用,但我遇到了问题

private List<String> musicGenreList = new ArrayList<>();
ObservableList<String> observableList = FXCollections.observableArrayList(musicGenreList);
private ListProperty<String> musicGenre = new SimpleListProperty<String>(observableList);

这是我向 ListProperty 添加项目的唯一方法,但 observableList 无法序列化

最佳答案

就像 James_D 所说,基本上我必须创建自定义 WriteExternalReadExternal

Users.java中需要进行更改

public class Users implements Externalizable
{
public Users(String userName, String firstName, String lastName, String city, LocalDate birthday)
{
setUserName(userName);
setFirstName(firstName);
setBirthday(birthday);
}

private IntegerProperty userID = new SimpleIntegerProperty(this, "id");
private StringProperty userName = new SimpleStringProperty(this, "userName');
private ObjectProperty<LocalDate> birthday = new SimpleObjectProperty<>(this, "birthday");

//Create setters and getters...

@Override
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeInt(getUserID());
out.writeObject(getUserName());
out.writeObject(getBirthday());
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
setUserID((int) in.readInteger());
setUserName((String) in.readObject());
setBirthday((LocalDate) in.readObject());
}
}

但是我无法使用以下方法序列化 ListProperty

private ObservableList<String> observableList = FXCollections.observableArrayList();
private ListProperty<String> newList = new SimpleListProperty<>(this, "musicGenre", observableList);

根据我的需要,ListProperty 需要以这种方式实例化,因为我需要使用 user.newListProperty().get().add("String") 向其中添加字符串项,并且无需使用ObservableList 我不能这样做

如果有人知道解决这个问题的方法,我们将不胜感激

关于java - ObjectInputStream 返回一个空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49328287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com