Java 我无法将数据写入通道网络

Java 我无法将数据写入通道网络,java,netty,Java,Netty,我对内蒂的研究还不熟悉。我想知道为什么我不能向服务器发送消息。MainSocket类隐藏主套接字 方法,如openConnect和outData public abstract class MainSocket { private final static String HOST = "localhost"; private static final int PORT = 8790; private EventLoopGroup workerGroup ; private Channel c

我对内蒂的研究还不熟悉。我想知道为什么我不能向服务器发送消息。MainSocket类隐藏主套接字 方法,如openConnect和outData

public abstract class MainSocket {
private final static String HOST = "localhost";
private static final int PORT = 8790; 

private EventLoopGroup workerGroup ;
private Channel channel;
private ChannelFuture future;
private Bootstrap bootstrap;


public  void  sendData(ByteBuf out){
    outData(out);
}
public void connect(){ openConnection(); }


public abstract void  sendMessage(String msg);
public abstract void  createConnect();

/*
Here I am trying to write into the channel*/
private   void outData(ByteBuf out){

    future= channel.writeAndFlush(out);
    //future.channel();
}

private void openConnection () {
    workerGroup = new NioEventLoopGroup();
    try {
        bootstrap = new Bootstrap();
        bootstrap.group(workerGroup).channel(NioSocketChannel.class)
                .handler(new SocketInit());
        future = bootstrap.connect(HOST,PORT).sync();

        future.channel().closeFuture().sync();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
    }
}

}
类继承者将字符串转换为字节Byf

    public class SocketClient extends MainSocket {
    private MainSocket clientSocket;
    ClientEncoder clientEncoder;
    @Override
    public void createConnect() {
    connect();

}

@Override
public void sendMessage(String msg) {
    final String msga = msg;
   ByteBuf data = Unpooled.wrappedBuffer(msga.getBytes());
   sendData(data);
}


}
错误堆栈

  xception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEve n tDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at  com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at      com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Glass    ViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Glass    ViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Glas    sViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.jav    a:389)
at   com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventH    andler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
 Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at azuki.com.connection.MainSocket.outData(MainSocket.java:40)
at azuki.com.connection.MainSocket.sendData(MainSocket.java:28)
at azuki.com.connection.SocketClient.sendMessage(SocketClient.java:26)
at     azuki.com.controller.MessageController.sendMessage(MessageController.java:43)
... 58 more

Process finished with exit code 1

我相信您的问题在于openConnection:

   future.channel().closeFuture().sync();
因为这将无限期地等待通道在返回之前关闭,除非发生异常

因此,空指针异常可能是因为您的通道未分配/连接(超时?)。
您必须将closeFuture().sync()移动到其他地方,在那里您可以安全地等待频道关闭(显然也要关闭它)。

您需要给出您看到的错误/问题。@Aaron在第future=channel.writeAndFlush(out)行上离开异常-nullpointerexception;在此代码中,您从未为频道分配值,您发布的代码中是否缺少某些内容?如果没有,那就是问题所在。最好是编辑问题以包含例外情况。不要把它留在评论里。