简体   繁体   中英

Java Chat Program: Client Class not working due to null pointer exception

Client class:

public void connect(String user){
            try{
                host = socket.getInetAddress();
                socket = new Socket(host,port);

            }

        }

The Login class:

login.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {

                String user = textInput.getText();
                Client client = new Client(username, 7777);
                client.setVisible(true);
                try{
                  client.connect(username);
                }
                catch(NullPointerException e){
                  e.printStackTrace();
                }
        }

    });

I'm calling the connect from the Client class and I tried catching the NullPointerException but I still get the error. I also tried inserting a null check instead of the try and catch clause I've tried to look around but I haven't found any answers. Can somebody tell me whats wrong please? What I want is the client to successfully connect.

您可以确保在调用connectClient()之前启动clientSocket。

Originally, it appeared there was a local variable declaration. Comments indicate perhaps that was incorrect. The issue is therefore that the Client class instantiated an object, but the Login class did not obtain a reference to it.

EDIT2 : after additional discussion, it appears that despite having a main method, the actual startup class was the Login class, rather than the Client class. As a result, the client object was not instantiated.

Moving the instantiation to the Login class resolved the NPE.

public class Login {
  //declare and instantiate a Client object
  static Client client = new Client();
 ....

  loginB.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {

            String username = usernameTF.getText();
            ClientGUI clientgui = new ClientGUI(username, 7777);
            clientgui.setVisible(true);
            try{
              // get the instantiated client
              System.out.println("Attempting connection for " + username +  
                "(client == null): " + (client == null ? "true" : "false"));

              // use the static variable
              client.connectClient(username);
            }
            catch(NullPointerException npe){
              npe.printStackTrace();
            }
    }

});
}

You have to change your connectClient method

public void connectClient(String user){
        try{
            //clientSocket is not instantiated yet
            host = InetAddress.getByName("127.0.0.1");
            clientSocket = new Socket(host,port);
            String connected = user+" just connected";
            clientGUI.appendMessage(accepted+"\n"+connected);
            serverGUI.appendEventsLog(accepted+"\n"+connected);

        new ClientHandler().run();

        }
        catch(Exception e){
            sendMessage("Connection error: "+e);
            serverGUI.appendEventsLog("Client "+new ClientGUI(username, port)+" failed to connect");
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM