使用JavaFXGUI联网时出现空指针异常

使用JavaFXGUI联网时出现空指针异常,java,networking,nullpointerexception,chat,javafx,Java,Networking,Nullpointerexception,Chat,Javafx,很抱歉,我很难找到我应该怎么称呼它 这就是交易!我目前正在创建一个聊天应用程序,其中我使用我在JavaFx中创建的Gui(一个有一些图形的Gui,但我认为这有点不相关),到目前为止我所做的是,我已经设置了一个小型服务器,每个客户机都通过该程序连接到它!主要的想法是客户端向服务器发送消息,然后服务器将消息发送给另一个客户端(这是聊天程序的全部想法)。一个重要的注意事项是,我没有使用线程,也不希望使用线程 因此,要深入了解真正的问题: 我创建了一个客户端类,其中包含连接、接收和发送的方法。我的Con

很抱歉,我很难找到我应该怎么称呼它

这就是交易!我目前正在创建一个聊天应用程序,其中我使用我在JavaFx中创建的Gui(一个有一些图形的Gui,但我认为这有点不相关),到目前为止我所做的是,我已经设置了一个小型服务器,每个客户机都通过该程序连接到它!主要的想法是客户端向服务器发送消息,然后服务器将消息发送给另一个客户端(这是聊天程序的全部想法)。一个重要的注意事项是,我没有使用线程,也不希望使用线程

因此,要深入了解真正的问题:

我创建了一个客户端类,其中包含连接、接收和发送的方法。我的Connect类与我的Gui配合得很好,我可以毫无问题地连接到服务器

当我尝试发送到服务器或从服务器接收时,问题就开始了。无论我抛出多少异常或尝试捕获多少异常,我都会得到一个空指针错误!我已经看了大约2个小时的代码,试图找出问题,但没有运气!我的代码如下:

客户端类:

private PrintWriter pw;
/**
 * @param args
 * @throws IOException 
 */
public void connect() throws IOException{
    final int portNumber = 6040;

    // du kan vælge at bruge inetadressen til at connecte i socketet.
    InetAddress adr = InetAddress.getByName("localhost");
    Socket socket = new Socket("localhost", portNumber);
    pw = new PrintWriter(socket.getOutputStream());
    // outPut - Programmet sender til serveren

    pw.println("Connected waiting for input");
    pw.flush();
    //input - Serveren sender til programmet;
}
public void Send(String x) throws IOException{
    if (x != null) {
        pw.print(x);
        pw.flush(); 
    }else {
    System.out.println("ingen meddelse");
    }

}
public String getInformation(){

    Scanner informationFromServer = new Scanner(System.in);
    String x = informationFromServer.nextLine();
    if (x== null) {
        return "";
    }
    return x;
}
我的SimpleControl代码(控制我的GUI的代码):

当我尝试发送时得到的异常当然在我的client.send()方法中,如果我尝试在发送之前接收,那么它在client.getInformation()方法中


我做错了什么?我遗漏了什么?

没有人知道为什么它会给我空点执行选项:(已编辑:向TAGSHow添加了nullpoint Exection您知道连接是否正确吗?连接失败将导致套接字变量中出现空值。我有一个程序测试连接,它告诉我我的客户端已连接到服务器。我看不到您在任何时候设置“客户端”成员变量的值我们在控制器中。这大概就是为什么在客户端上获得NPE。send()-客户端为空。
public class SimpleController implements Initializable{

public Button btn_Connect;
private Client client;
public Label chatPerson3;
public Label chatPerson1;
public Label lbl_Chatperson1_userName;
public TextField txt_userName;
public TextField textField_chat;
public TextField txt_ChatPerson1;
public Button Send;
public TextField txt_ChatPerson2;
@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub


    btn_Connect.setOnAction(new EventHandler<ActionEvent>() {   
        @Override
        public void handle(ActionEvent event) {

            chatPerson1.setVisible(true);
            lbl_Chatperson1_userName.setText(txt_userName.getText());
        }
    });

    Send.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {

            String x = textField_chat.getText();
            textField_chat.setText("");
            txt_ChatPerson1.setVisible(true);
            txt_ChatPerson1.setText(x);

            System.out.println(x);
            try {
                client.Send(x);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

}}
public class Main extends Application{



public static void main(String[] args) throws IOException{
    Application.launch(Main.class, (java.lang.String[]) null);

}

@Override
public void start(Stage primaryStage) throws Exception {
    try {
        Client c = new Client();
        c.connect();
        AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("testingBackground.fxml"));
        Scene scene = new Scene(page);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Chatten");
        primaryStage.show();

    } catch (Exception ex) {
        java.util.logging.Logger.getLogger(Main.class.getName()).log(
                java.util.logging.Level.SEVERE, null, ex);

    }

}

}