So I'm trying to send an Object from a client to the server and then I do something with this object. I verified that when being sent the object contains data. When I'm in the debugger reading my object all of its properties are null.
Below is where I send the object:
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;
}
The above task is then used when I press a button in my GUI as shown below:
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();
}
The code in the server where object is received:
//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);
And the User.java class which is the object at debate
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);
}
}
I've left out the getters and setters as this questions is getting long already
So in the debugger when running the server all the properties of the user are null when I doSomething(user)
*EDIT The suggestions are working out but I've gotten a problem with
private List<String> musicGenreList = new ArrayList<>();
ObservableList<String> observableList = FXCollections.observableArrayList(musicGenreList);
private ListProperty<String> musicGenre = new SimpleListProperty<String>(observableList);
It's the only way I can add items to the ListProperty
but the observableList
can't be serialized