0

I've got an issue with seperating business logic from controller (apart from that how i'm validating data in my program).

Here is logic class:

public class LogInMechanism {

LogInController logInController = new LogInController();
private static int howManyAttempts=0;

public void logIn()
{
howManyAttempts++;
if(howManyAttempts <3)
{
    if(logInController.getLogInField().getText().equals("nick") && logInController.getPasswordField().getText().equals("password"))
    {
        System.out.println("Button clicked");
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/fxml/MainWindow.fxml"));
            logInController.getLogInField().getScene().setRoot(root);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    else
    {
        logInController.getInvalidUserOrPasswordLabel().setText("Invalid user or password "+(3-howManyAttempts)+ " attempts left!");
        logInController.getInvalidUserOrPasswordLabel().setVisible(true);
    }
}
else
{

    logInController.getInvalidUserOrPasswordLabel().setDisable(true);
    logInController.getInvalidUserOrPasswordLabel().setText("Your program has been blocked. Please contact manager!");


}
}


}

And i want this function to be used in one of my controllers

public class LogInController {
private LogInMechanism logInMechanism = new LogInMechanism();

@FXML
private Label invalidUserOrPasswordLabel;

@FXML
private TextField logInField;

@FXML
private Button logInButton;

@FXML
private PasswordField passwordField;

@FXML
void logInButtonClicked(ActionEvent event) {
    logInMechanism.logIn();
}

@FXML
public void initialize()
{
    EventHandler<KeyEvent> handlerLambda = e ->{
        if(e.getCode()==KeyCode.ENTER)
        {
        //  logInMechanism.logIn();
        }

    };
    logInButton.addEventHandler(KeyEvent.KEY_PRESSED, handlerLambda);
}

public Label getInvalidUserOrPasswordLabel() {
    return invalidUserOrPasswordLabel;
}

public TextField getLogInField() {
    return logInField;
}

public PasswordField getPasswordField() {
    return passwordField;
}

But unfortunatelly ifter performing such code i recieve an error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Exception in Application start method
Caused by: java.lang.StackOverflowError

The error appears at line 19 in LogInMechanism class. I'm still learning though, if you have any suggestions please share.

Mix1234
  • 11
  • 5
  • You seem to have a recursive call somewhere. Look into the stack trace that came with the exception and try to look at any method name that is repeated – litelite Jul 04 '17 at 20:21
  • at pl.karkowskiCompany.app.LogInMechanism.(LogInMechanism.java:11) at pl.karkowskiCompany.controller.LogInController.(LogInController.java:19) – Mix1234 Jul 04 '17 at 20:24
  • 1
    Your `LogInMechanism` is creating a `LogInController` which when it is created will in turn create a `LogInMechanism` which will again create a `LoginController` and so on. That's whats causing your StackOverflow. One of the two class should not have a `new` – litelite Jul 04 '17 at 20:29
  • It solved a part of my problem, which i was asking in this question. Thanks a lot – Mix1234 Jul 04 '17 at 20:31
  • Tell, Don't ask... What's wrong with passing the password/username to the `login` method??? Also you probably should seperate the layers differently: Write some class that allows you to activate a new view passing nothing but an identifier for the scene to activate (probably best to not even use the fxml path). Also you should probably pass the `LoginMechanism` to the controller instead of creating a new one in the initializer which would make using `static` unnecessary and thus improve code quality... – fabian Jul 04 '17 at 21:22

0 Answers0