I have a class name player
public JLabel imagen;
public String Nombre;
public Player(int x, int y, int width, int height, Icon icono, String name){
imagen = Player(x, y, width, height, icono);
Nombre = name;
}
public JLabel Player(int x, int y, int width, int height, Icon icono){
JLabel imagen = new JLabel(icono);
imagen.setLocation(x, y);
imagen.setSize(width, height);
return imagen;
}
(It is for creating a new player)
I also have a client class:
public class Cliente implements Runnable {
String host;
int puerto;
Player mensaje;
public Cliente(int purto, Player mensaje, String host){
this.puerto = purto;
this.mensaje = mensaje;
this.host = host;
}
@Override
public void run() {
DataOutputStream out;
try {
Socket sc = new Socket(host, puerto);
out = new DataOutputStream(sc.getOutputStream());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
objectOutputStream.writeObject(mensaje);
sc.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
And im using objectOutputStream but it says that it
"java.io.NotSerializableException: objects.Player"
And I want to send my player to the server but it says that exception!
Also if you need here is the server class
public class Servidor extends Observable implements Runnable {
int puerto;
public Servidor(int puerto) {
this.puerto = puerto;
}
@Override
public void run() {
ServerSocket servidor = null;
Socket sc = null;
DataInputStream in;
try {
servidor = new ServerSocket(puerto);
System.out.println("server started");
while (true) {
sc = servidor.accept();
in = new DataInputStream(sc.getInputStream());
ObjectInputStream input = new ObjectInputStream(in);
Player players = null;
try {
players = (Player) input.readObject();
System.out.println(players.Nombre);
} catch (ClassNotFoundException ex) {
}
this.setChanged();
this.notifyObservers(players);
this.clearChanged();
sc.close();
}
} catch (IOException ex) {
}
}
}
and also if you want here are the lines of code that send the request to the client class
Cliente c = new Cliente(5000, new Player(x, y, width, height, icon, "name of the player"), "the ip");
Thread t = new Thread(c);
t.start();