1

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.

Guest
  • 83
  • 2
  • 9
  • 2
    Every NPE has the same cause: the object is null. As far as I can tell, you have not provided any code that indicates where the `client` object is instantiated. Also, I would suggest modifying the title to indicate the problem. – KevinO Apr 20 '16 at 17:18
  • you can refer to [this](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it/218510#218510) question, you can edit your post and add the code that instantiates `client`(and preferably `clientGUI` too) – niceman Apr 20 '16 at 17:21
  • @niceman I instantiated the clientGUI inside the actionlistener – Guest Apr 20 '16 at 17:23
  • And what does the `clientgui` object have to do with the `client` object? – KevinO Apr 20 '16 at 17:24
  • @KevinO I've edited the code to include the Client class. I instantiated the Client object inside the main method of the client class. – Guest Apr 20 '16 at 17:24
  • There is *no way* for a local variable in the `static main` to have any effect on the `client` object in a handler. *If* there is a static client variable declared somewhere (again, not shown in the code), then change `Client client = new Client(...)` to 'client = new Client(...)` in the `main` method. – KevinO Apr 20 '16 at 17:27

3 Answers3

0

Can you make sure that clientSocket is initiated before calling connectClient().

Cool Geek
  • 79
  • 2
  • 7
  • It is an NPE, so if the `connectClient()` failed I would expect a different Exception (unless, I suppose `username` was null). – KevinO Apr 20 '16 at 17:25
  • the reason I instantiate `clientSocket` inside the `connectClient()` method is because the socket isn't supposed to open before the client connects. I've done some research into other chat/server programs and the socket is instantiated in some sort of "start connection" method – Guest Apr 20 '16 at 17:50
0

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

});
}
KevinO
  • 4,303
  • 4
  • 27
  • 36
  • Sorry for the confusion and thanks for your patience; I haven't declared any variable inside any main class. The only thing I've done is instantiated the Client object inside the main method of the Client class. – Guest Apr 20 '16 at 17:33
  • After the edit you made, you changed the definition. You clearly had in the `main` method `Client client = new Client(...);` If that was a typo, OK, but it really was what you previously had. Nonetheless, if your edit is correct, how is the `Login` class going to have the variable that you defined in the `Client` class? – KevinO Apr 20 '16 at 17:36
  • @Guest, I have updated the answer based upon your comments. Basically, if `main` instantiates the client, but the `Login` class needs it, you will need a mechanism for the `Login` class to obtain it. There are many ways, but one example is shown where the `Login` calls a new method in `Client` to obtain it. – KevinO Apr 20 '16 at 17:41
  • I edited the code to include the getter method in the `Client` class and updated the `Login` class too, I still get an exception. Do you reckon the problem could in any way be with the `username` variable being called inside the `connectClient()` method? – Guest Apr 20 '16 at 17:46
  • @Guest, you could add the stack trace for the NPE. You can also do a `System.out.println("Attempting to connect to client for " + username + " (client == null): " + (client == null));`just before the `.connectClient()`. This would indicate both if we have an instantiated client as well as the value of `username`. – KevinO Apr 20 '16 at 17:49
  • I added your line of code to the action listener. So the username is fine, but it says `(client == null)` gives me a `true` value which means the client has not been instantiated. – Guest Apr 20 '16 at 18:00
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109714/discussion-between-kevino-and-guest). – KevinO Apr 20 '16 at 18:04
0

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

    }
J-M
  • 103
  • 5
  • `clientSocket` has been instantiated if you read the code properly, it says `clientSocket = new Socket (host, port);` – Guest Apr 20 '16 at 17:49
  • @Guest you are instantiating host from clientSocket and then clientSocket from host... `host = clientSocket.getInetAddress(); clientSocket = new Socket(host,port);` you have circular dependency there and it won't work. – J-M Apr 21 '16 at 07:47
  • then what do you suggest I do? – Guest Apr 21 '16 at 15:13