I have a server and a client. The client sends a request to the server, and the latter in turn makes a request to the database and receives a ResultSet. Then it forms a list of objects using the ResultSet and serializes from to the buffer, then to the socket. The client, in turn, must receive this information and deserialize a list of objects. I was getting a ClassNotFoundException error. Then I began to transfer not a list but one object. The problem did not go away. Both on the server and on the client there is a class which is serialized, they are completely identical.
Here is the class:
package main; import java.io.Serializable; public class Messag implements Serializable { String login; String time; String message; Messag(String login,String time,String message){ this.login = login; this.time = time; this.message = message; } @Override public String toString(){ return login + "/" + time + "/" + message; } } Server:
while (resSet.next()){ Messag message = new Messag(resSet.getString(1),resSet.getString(2),resSet.getString(3)); listMessage.add(message); } /* for (Message message : listMessage) { System.out.println(message.toString()); } */ Messag msg = listMessage.get(1); ObjectOutputStream oos = new ObjectOutputStream(baosSerializable); oos.writeObject(msg); ByteBuffer buffer = ByteBuffer.wrap(baosSerializable.toByteArray()); System.out.println(baosSerializable.toByteArray().length + " Длина буфера"); while (buffer.hasRemaining()) sc.write(buffer); The length is displayed on the server and on the client to make sure that everything comes (And everything comes =)) Client:
ByteArrayOutputStream baos = new ByteArrayOutputStream(); for ( int cnt; (cnt = clientSocket.read(buf.clear())) != -1; ) baos.write(buf.array(),0, cnt); System.out.println(baos.toByteArray().length + " Длина буфера"); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); ois.getClass().getClassLoader().loadClass("Messag"); Messag msg = (Messag)ois.readObject(); The error is displayed here: ois.getClass (). GetClassLoader (). LoadClass ("Messag"); and NullPointerException error. If this line is removed, then there will be an error on the next line ClassNotFoundException ((