java.io.NotSerializableException:: ObjectOutputStream: writeObject method <p>I am trying to connect client to a server using sockets.</p> <p>Server code:</p> <pre><code>ServerSocket server = new ServerSocket(6780); Socket clientSocket = server.accept(); ObjectInputStream fromClient= new ObjectInputStream(clientSocket.getInputStream()); ObjectOutputStream toClient = new ObjectOutputStream(clientSocket.getOutputStream()); </code></pre> <p>Client code:</p> <pre><code>Socket nodeSocket = new Socket(ServerIP, 6780); ObjectInputStream fromServer = new ObjectInputStream(nodeSocket.getInputStream()); ObjectOutputStream toServer = new ObjectOutputStream(nodeSocket.getOutputStream()); toServer.writeObject(this); </code></pre> <p>When a client connects, server listens and accepts client connection. But when I try to write my class object to the server, "toServer.writeObject(this);" it throws</p> <pre><code>java.io.NotSerializableException: Peer at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at Peer.requestEntryNode(Peer.java:47) at Peer.main(Peer.java:87) </code></pre> <p>Peer is my class name.</p> <p>I tried to find out the cause of this exception, it says that object that needs to be written must be serializable. I also tried to implement Serializable interface from Peer class. But that doesnt help.</p> <p>Can anyone suggest?</p>