Note that there are some explanatory texts on larger screens.

plurals
  1. PONotSerializableException when sending an serializable object over socket?
    text
    copied!<p>Im trying to send a custom-made object over a socket connection. The class implements serializable but the constructor still throws a <code>NotSerializableException</code> when im trying to write the object to the socket. I'll post relevent code below:</p> <pre><code>public class serilizableArrayHolder implements Serializable { private ArrayList&lt;Client&gt; array= null; public serilizableArrayHolder(ArrayList&lt;Client&gt; array) { this.array=array; } public ArrayList&lt;Client&gt; getArray() { return array; } } </code></pre> <p>This is my custom-made class. For now im trying to send an arraylist from the server to the client but i'll add additional info in a later stage. The send method is posted below in my server class is posted below:</p> <pre><code>public void sendData(Socket clientSocket){ ObjectOutputStream out; try { serilizableArrayHolder temp = new serilizableArrayHolder(clientCollection); out = new ObjectOutputStream(clientSocket.getOutputStream()); out.writeObject(temp); &lt;---This line generates the error. out.flush(); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <p>This is my send-method from the server. <code>clientCollection</code> is the arrayList that im trying to send.</p> <p>The whole Client class:</p> <pre><code>public class Client implements Runnable, Serializable{ public Thread thread = new Thread(this); private Socket s; private DataInputStream in; private DataOutputStream out; private ObjectOutputStream objOut; private ServerMain server=null; private String host=null; private Client c; private String userName; public Client(Socket s, String host, ServerMain server) throws IOException{ c=this; this.host=host; this.s=s; this.server=server; this.userName=userName; in= new DataInputStream(s.getInputStream()); out=new DataOutputStream(s.getOutputStream()); objOut=new ObjectOutputStream(s.getOutputStream()); thread.start(); } public String getClientInfo(){ return host; } public String getUserName(){ return userName; } public void send(String s){ try { out.writeUTF(s); } catch (IOException e){ } } public void run() { while(true){ try { String temp = in.readUTF(); if(temp.equals("sendOnline")){ sendOnline(); } String tempHost=s.getInetAddress().getHostAddress(); server.appendString(tempHost+" Skickade: "+temp+"\n"); } catch (IOException e) { String str = s.getInetAddress().getHostName(); server.clientDisconnect(str); break; } } try { s.close(); } catch (IOException e) { } } public void sendOnline(){ serilizableArrayHolder temp = new serilizableArrayHolder(server.getClients()); try { objOut.writeObject(temp); objOut.flush(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Metoden anropas"); } </code></pre> <p>}</p> <p>The new stacktrace:</p> <pre><code>java.io.NotSerializableException: java.io.DataInputStream Metoden anropas at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) at java.util.ArrayList.writeObject(ArrayList.java:710) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) at Gesäll.Client.sendOnline(Client.java:83) at Gesäll.Client.run(Client.java:58) at java.lang.Thread.run(Thread.java:722) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload