0

对不起,我很难找到我应该怎么称呼它的标题。

所以这里是交易!我目前正在创建一个聊天应用程序,在其中使用我在 JavaFx 中创建的 Gui(一个带有一些图形的 Gui,但我认为这有点无关紧要)到目前为止我所做的是我已经设置了一个小每个客户端通过程序连接的服务器!主要思想是客户端向服务器发送一条消息,然后服务器将其发送给另一个客户端(这是聊天程序中的整个想法)一个重要的注意事项是我没有使用线程并且不希望然而!

所以要解决真正的问题:

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

当我尝试发送到服务器或从服务器接收时,问题就开始了。无论我抛出多少异常或尝试 Catch 多少次,我都会收到空指针错误!我已经查看了大约 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;
}

我的 simpleController 代码(控制我的 GUI 的代码):

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);

    }

}

}

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

我做错了什么?我错过了什么?

4

0 回答 0