使用JFrame表单的client_server聊天应用程序..初始化服务器的套接字

时间:2017-06-22 02:46:41

标签: java sockets nullpointerexception jframe

我使用java套接字创建了一个聊天应用程序,它在控制台中工作正常但是 在netbeans中使用JFrame表单为客户端创建接口时,我在connectButtonActionPerformed方法中不断获得空指针异常,尽管我已经初始化了将连接到服务器socket ss;的套接字: / p>

public ChatMain() {
    initComponents();
        try {
    this.ss = new Socket(host, port);
        /* obtain an output stream to the server... */
            DataOutputStream outToServer = new DataOutputStream(this.ss.getOutputStream());
            /* ... and an input stream */
            BufferedReader infromserver = new BufferedReader(new InputStreamReader(this.ss.getInputStream()));
    } catch (IOException ex) {
            System.out.println(ex.getMessage());


    }
}

这是整个客户端类:

public class ChatMain extends javax.swing.JFrame   {

private static BufferedReader inFromUser;
private static int port = 6789; /* port to connect to */
private static String host = "localhost"; /* host to connect to */
public  static  Socket ss;             
public static String username;

 BufferedReader infromserver;
DataOutputStream outToServer;
Boolean isConnected = false;


public ChatMain() {

    initComponents();
        try {
    this.ss = new Socket(host, port);
        /* obtain an output stream to the server... */
            DataOutputStream outToServer = new DataOutputStream(this.ss.getOutputStream());
            /* ... and an input stream */
            BufferedReader infromserver = new BufferedReader(new InputStreamReader(this.ss.getInputStream()));
    } catch (IOException ex) {
            System.out.println(ex.getMessage());


    }
}

private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    if (isConnected == false) {
        username = usernameField.getText();  
        usernameField.setEditable(false);


        try {

            outToServer.writeBytes(username + '\n');
            String serverResponse = infromserver.readLine();
            chatTextArea.append(serverResponse + '\n');
            if (serverResponse.equalsIgnoreCase("SERVER: OK")) {
                isConnected = true; // Used to see if the client is connected.

            }

        } catch (Exception ex) {
            chatTextArea.append("Cannot Connect! Try Again. \n");
             System.out.println(ex.getMessage());


            usernameField.setEditable(true);
        }
    } else if (isConnected == true) {
        chatTextArea.append("You are already connected. \n");
    }

}                                             


/**
 * @param args the command line arguments
 */
public static void main(String args[]) throws IOException {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new ChatMain().setVisible(true);
        }

    });
}



// Variables declaration - do not modify                     
private javax.swing.JTextArea chatTextArea;
public static javax.swing.JButton connectButton;
private javax.swing.JButton disconnectButton;
private javax.swing.JTextArea inputTextArea;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
public static javax.swing.JTextArea onlineUsersArea;
private javax.swing.JButton sendButton;
private javax.swing.JButton update;
public static javax.swing.JTextField usernameField;
// End of variables declaration                   

}

我知道如果我在connectButtonActionPerformed方法中初始化服务器套接字它将正常工作**但是我必须在每个操作方法中重新初始化它,据我所知我应该只使用一个连接到每个客户端的服务器** .. StackTrace

    java.lang.NullPointerException
at my_private_chatting.ChatMain.connectButtonActionPerformed(ChatMain.java:194)
at my_private_chatting.ChatMain.access$000(ChatMain.java:23)
at my_private_chatting.ChatMain$1.actionPerformed(ChatMain.java:89)

此行会在outToServer.writeBytes(username + '\n');方法中抛出异常connectButtonActionPerformed ... 提前谢谢

0 个答案:

没有答案