1

I'm working on the project which contains multiple controllers (1 main and two for each of the Fxml files). Unfortunately while running the program, console throw the NullPointerExeption at me. I found out that it's the main controller fault, but still having this information I'm unable to fix this. Could you give me some tips how to solve this issue ? Here i got the full track:

MainController class:

public class MainController {
  @FXML LogInController logInController;
  DBConnect dbConnect;
  @FXML FlightControlController flightControlController;

  @FXML public void initialize() {
    logInController.initialize(this);
    flightControlController.initialize(this);
    dbConnect.initialize(this);
  }
}

DBConnect class:

public void dbConnect() throws SQLException {
  try {
    Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nasa", "root", "1234");
  } catch(Exception e) {
    System.out.println(e.getStackTrace());
  }
}

public boolean isCorrect(String login,String password) throws SQLException, IOException {
  Stmt = Conn.createStatement();
  Rs = Stmt.executeQuery("SELECT * FROM Users WHERE login='"+login+"'AND password='"+password+"';");
  if(Rs.next()) {
    return true; 
  } else {
    return false;
  }
}

public void initialize(MainController mainController) {
  this.mainController=mainController;
}

LogInController class:

MainController mainController;
@FXML DBConnect dbConnect;
@FXML TextField logginField = null;
@FXML PasswordField passwordFiled=null;
@FXML Button logInButton;

@FXML
public void buttonClicked(ActionEvent event) throws SQLException, IOException {
  mainController.dbConnect.dbConnect();
  if(mainController.dbConnect.isCorrect(logginField.getText(),passwordFiled.getText())) {
    Parent root = FXMLLoader.load(getClass().getResource("/view/FlightControlView.fxml"));
    Scene scene = new Scene(root);
    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    stage.setScene(scene);
    stage.show();
  } else {
    System.out.print("An error have occured");
  }
}

public void initialize(MainController mainController) {
  this.mainController=mainController;
}

FlightControlController Class:

@FXML Label label;
MainController mainController;

public void initialize(MainController mainController) {
  this.mainController = mainController;
}

And the error which occurs:

Caused by: java.lang.NullPointerException at controller.LogInController.buttonClicked(LogInController.java:31) ... 62 more

I'm not sure but is it possible that MainController (which doesn't have his own Fxml file) so that FXMLLoader doesn't initialize MainController's properties?

@EDIT

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="693.0" prefWidth="1062.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LogInController">
   <children>
      <SplitPane dividerPositions="0.21320754716981133" layoutX="331.0" layoutY="220.0" prefHeight="693.0" prefWidth="1062.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
         <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
           <children>
                  <Label alignment="CENTER" layoutY="-1.0" maxHeight="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="70.0" prefWidth="222.0" text="NASA DATABASE" />
                  <TextField fx:id="logginField" layoutX="18.0" layoutY="101.0" promptText="Login" />
                  <PasswordField fx:id="passwordFiled" layoutX="18.0" layoutY="165.0" promptText="Password" />
              <Button fx:id="logInButton" onAction="#buttonClicked" layoutX="79.0" layoutY="230.0" mnemonicParsing="false" text="Log In" />
           </children>
        </AnchorPane>
      <AnchorPane prefHeight="691.0" prefWidth="493.0" SplitPane.resizableWithParent="false">
           <children>
              <Label alignment="CENTER" layoutX="97.0" layoutY="104.0" prefHeight="417.0" prefWidth="635.0" text="NOT AVAILABLE, PLEASE LOG IN" AnchorPane.leftAnchor="113.0" AnchorPane.rightAnchor="113.0">
                 <font>
                    <Font name="System Bold Italic" size="37.0" />
                 </font>
              </Label>
           </children>
        </AnchorPane>
    </items>
  </SplitPane>
   </children>
</AnchorPane>
7
  • Is the initialize method in main controller derived from javafx.fxml.Initializable? If not it might not be executed. Commented Sep 27, 2016 at 14:36
  • Can you post the FXML file where you use this? You write that your MainController does not have its own FXML, so I suspect that's why the button is not injected. Only a controller assigned to a FXML document is injected with @FXML fields Commented Sep 27, 2016 at 14:37
  • Maybe i missunderstood the idea of MVC and multiple controllers. I thought that both of controllers from fxml files shoudnt know each other exist. They should be connected only by MainController, so this is what I did. How could i create a fxml files for MainController for which his only duty is to make available communication between rest of controllers Commented Sep 27, 2016 at 14:45
  • Could you maybe change your structure around? The main controller is the assigned FXML-controller; whenever an event occurs, the main controller collects relevant data and sends it to the controller that needs it. The sub-controller then returns some kind of data, so the main controller knows what do display. Commented Sep 27, 2016 at 14:52
  • So generally speaking. In JavaFx there shouldn't exist controller without FXML file ? Am i correct? Commented Sep 27, 2016 at 15:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.