0

I have a problem with nullPointer while i'm trying to modify any element from second Panel in my application. How my application look like: I have 2 Panel, first to loggin and second one with Main Application. Steps in my application:

  1. Create and show 1 panel
  2. Provide passwords and loggin to database a/ Success - open new panel and hide this one b/ show error message
  3. Now i'm trying to modify any element from panel 2 but i cant
Caused by: java.lang.NullPointerException
    at sample.ControllerLoginMenu.openScene(ControllerLoginMenu.java:114)
    at sample.ControllerLoginMenu.login(ControllerLoginMenu.java:41)
    ... 58 more

Here is my code

public class ControllerLoginMenu {
@FXML private Label statusMessage; // second Panel
@FXML private Circle statusCircle; // second Panel
@FXML private Label errorMessageConnection; // First Panel
@FXML private Label errorMessagePassword;   // First Panel
@FXML private TextField loginField;         //First Panel
@FXML private PasswordField passwordField;  //First panel
private String username ;
private String password ;
private final String url = "jdbc:oracle:thin:@google.com:6666:NotExist";
private Stage primaryStage = new Stage();
private Connection con;
private Statement statement;
@FXML private void login(ActionEvent event) throws IOException {
    checkConnection();
    openScene(event);
    // From there is the problem'
    errorMessageConnection.setVisible(true); -- no Error
    statusCircle.setFill(Color.RED); -- Error
}
private void openScene(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
    ((Node) event.getSource()).getScene().getWindow().hide();
    primaryStage.setTitle("Masterdata loading application");
    primaryStage.setScene(new Scene(root, 640, 480));
    primaryStage.show();
}
private boolean checkConnection()   {

    boolean result = false;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return result;
    }
    try {
        con = DriverManager.getConnection(url, username, password);
        statement = con.createStatement();
        String sql = "SELECT SYSDATE FROM DUAL";
        result  = statement.execute(sql);
    }
    catch (SQLException e)
    {
        passwordField.setText("");
        e.printStackTrace();
        return result;
    }
    return result;
}
private boolean getPasswords()
{
    username = loginField.getText();
    password = passwordField.getText();
    if(username.isEmpty() || password.isEmpty())
        return false;
    else
        return true;
}
@Override // Main Class
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("LoginMenu.fxml"));
    primaryStage.setTitle("Masterdata loading application");
    primaryStage.setScene(new Scene(root, 540, 380));
    primaryStage.show();
}

I other posts i have found that this should solve problem, but it didnt.

Platform.runLater(() ->{
statusCircle.setFill(Color.GREEN);});
hakobot
  • 186
  • 2
  • 9
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JFPicard Jan 17 '17 at 15:24
  • 1
    Not really a duplicate of that question (though it's probably very useful for the OP to read it); it doesn't address the case of fields that are supposed to be initialized by frameworks, which is the case here. – James_D Jan 17 '17 at 19:27

1 Answers1

1

You load two fxml files, so you have (at least) two controllers. The fields with matching fx:ids in the first fxml file will be initialized in the first controller, and the fields with matching fx:ids in the second fxml file will be initialized in the second controller. Since errorMessageConnection and statusCircle are defined in different FXML files, it is impossible for them both to be non-null in the same controller. Consequently, your login method is guaranteed to throw a null pointer exception.

(You actually appear to be getting a null pointer exception in openScene, but there isn't enough information in your question to diagnose that. It is likely related.)

If you use the same controller class for all your different fxml files, it becomes extremely difficult to keep track of what is initialized, and what remains null, in each controller. You should use a different controller class for each FXML file. If you need to pass data to a controller, use the techniques outlined in this answer.

Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322