'Netty remoteAddress from channel is null
The following code is from a piece from a extended class of ChannelInboundHandlerAdapter:
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channel inactive, reconnect to the server.");
long sleepTimeMs = 1000;
final EventLoop eventLoop = ctx.channel().eventLoop();
SocketAddress socketAddress1 = ctx.channel().remoteAddress();
eventLoop.schedule(() -> {
try {
System.out.println("Reconnecting ...");
tcpClient.connect(ctx.channel().remoteAddress());
} catch (Throwable e) {
e.printStackTrace();
}
}, sleepTimeMs, TimeUnit.MILLISECONDS);
ctx.fireChannelInactive();
}
Without having
SocketAddress socketAddress1 = ctx.channel().remoteAddress();
the following line would throw a NullPointerException:
tcpClient.connect(ctx.channel().remoteAddress())
as the ctx.channel().remoteAddress() param is null (I tested it multiple times with and without socketAddress1 assignment).
Any explanation of this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
