title: Netty How to send an object
body: <p>How to send an object using netty. When i try to send a String object everything is ok.</p>
  
  <pre><code>channel.writeAndFlush(new String("df"));
  </code></pre>
  
  <p>But when i try to send an simple object server get errors</p>
  
  <pre><code>public class Message implements Serializable {
  
      private static final long serialVersionUID = 1L;
  }
  channel.writeAndFlush(new Message()); 
  </code></pre>
  
  <hr>
  
  <pre><code>io.netty.channel.DefaultChannelPipeline$TailHandler exceptionCaught
  WARNING: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
  io.netty.handler.codec.DecoderException: java.lang.NullPointerException
      at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:259)
      at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:141)
      at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:340)
      at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:326)
      at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)
      at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:116)
      at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:494)
      at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:461)
      at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:378)
      at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
      at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
      at java.lang.Thread.run(Thread.java:744)
  Caused by: java.lang.NullPointerException
      at io.netty.handler.codec.serialization.CompactObjectInputStream.readClassDescriptor(CompactObjectInputStream.java:55)
      at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1601)
      at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at io.netty.handler.codec.serialization.ObjectDecoder.decode(ObjectDecoder.java:73)
      at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:343)
      at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:228)
      ... 11 more
  </code></pre>
  
  <hr>
  
  <pre><code>class ChatServerInitializer extends ChannelInitializer&lt;SocketChannel&gt; {
  
      @Override
      protected void initChannel(SocketChannel arg0) throws Exception
      {
          ChannelPipeline pipeline = arg0.pipeline();
          pipeline.addLast("decoder", (new ObjectDecoder(null)));  
          pipeline.addLast("encoder", new ObjectEncoder());  
          pipeline.addLast("handler", new ChatServerHandler());
      }
  }
  </code></pre>