0

I have a problem with connect. I can't connect between client in Start and client in Login. When I run app, It is NullPointerException. Why is connect between client in Start and client in Login?

public class LoginWindow extends Application{
    Client client = new Client("localhost", 3333);
    @FXML
    private TextField Username;
    @FXML
    private PasswordField Password;
    @FXML
    private Button login;


    public void Login(ActionEvent event) throws IOException {
        Stage stage;
        Parent root;
        //client.connect();
        String username = Username.getText();
        String password = Password.getText();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        //if (username.equals("user") && password.equals("pass")){
        if (client.Check_Login(username, password)) {
            stage = (Stage) login.getScene().getWindow();
            root = FXMLLoader.load(getClass().getResource("ChatView.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        } else {
            alert.setContentText("Login failed");
            alert.show();
        }
    }

   @Override
    public void start(Stage primaryStage) throws Exception {
        client.connect();
        Parent root = FXMLLoader.load(LoginWindow.class.getResource("LoginWindow.fxml"));
        primaryStage.setTitle("Chat 419");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }
}

  • 1
    It looks like you are trying to use the `Application` class as the class for your controller? That's usually (perhaps always) a mistake. But anyway, which variable is null? – James_D May 12 '20 at 13:08
  • line: client.connect() and line: client.Check_Login. It's not synchronized – Dương Khắc Linh May 12 '20 at 13:14
  • 1
    Assuming you are using this class *both* as the application life-cycle class *and* as the class for the controller, the `client` in `start()` is a different object to the `client` in `Login()`. I don't know what happens in your `Client` constructor when you create the second instance with the same host and port as the first instance. Just don't use the same class for the application and the controller, it's too confusing. – James_D May 12 '20 at 13:19
  • I created a application and a controller. In application, i also call client.connect() - which to connect to server but it still is error. client is also 2 different object. How sync it? – Dương Khắc Linh May 12 '20 at 13:26
  • this is my project: https://github.com/minhthienmu/419_0.1 – Dương Khắc Linh May 12 '20 at 13:28
  • I don't know what you mean by "How sync it?". I also don't know what happens when you create two different `Client` objects with the same host and port, which, assuming you use this class as the controller class in the FXML, you are doing here. They certainly can't both listen on the same port. – James_D May 12 '20 at 13:28
  • Is there any way to make it an object? I want when i run client.connect(), it will connect to client.check_login – Dương Khắc Linh May 12 '20 at 13:35
  • I don't know what you mean by that either. Your `Client client = new Client("localhost", 3333);` line creates an object (unless it throws an exception). But again, start by using a different class for your controller than you are using for the application. – James_D May 12 '20 at 13:37
  • 1
    The problem (probably: there isn't sufficient code in your question to tell, and I'm not reading your repo, because your question should stand alone to be useful to other users) is most likely that the `Client` instance on which you're calling `Check_Login()` isn't connected, because you never call `connect` on that instance (you call it on a different instance). – James_D May 12 '20 at 13:42

0 Answers0