NPE когда пытаюсь отправить сообщение на сервер
Мне нужно из полей loginField и passwordField (JavaFX) взять текст и отправить его на сервер используя Netty. Но когда клиент берет данные из полей и пытается отправить их на сервер - выскакивает NullPointerExeption. Помогите разобраться, пж :)
Код клиента
public class NetworkConnector {
private static final int PORT = 9999;
private static final String LOCAL_HOST = "localhost";
SocketChannel channel;
public void connect() {
new Thread(() -> {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
channel = socketChannel;
socketChannel.pipeline().addLast(new StringDecoder(), new StringEncoder());
}
});
ChannelFuture future = bootstrap.connect(LOCAL_HOST, PORT).sync();
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}finally {
workerGroup.shutdownGracefully();
}
}).start();
}
public void sendMessage(String str){
channel.writeAndFlush(str);
}
}
Код отправки сообщения с клиента на сервер
public void regInDbAction(ActionEvent actionEvent) {
networker.sendMessage(loginField.getText());
}
Код сервер
public class NettyServer {
private static final int PORT = 9999;
EventLoopGroup bossGroup = new NioEventLoopGroup(10);
EventLoopGroup workerGroup = new NioEventLoopGroup();
public void start(){
try{
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture future = bootstrap.bind(PORT).sync();
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
Код ServerHandler
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("Client connected");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buffer = (ByteBuf) msg;
while(buffer.readableBytes() > 0){
System.out.println((char) buffer.readByte());
}
buffer.release();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}