0

My application is throwing a NullPointer exception when I call the variable empName.

            ResultSet result = stmnt.executeQuery("select FirstName, LastName from emp_info where EmployeeID "
                    + "= '" + empID + "'");

//            while(result.next()){
//                empName.setText("result.getString(1));
//            }
            empName.setText("asdf");
            rootLayout.setCenter(controlData);

            connection.close();
        }

    }

It should be able to run fine since I initialize the Text variable like so:

@FXML
public Text empName;

Also when I use scenebuilder, it sometimes shows the fx:id empName but when I exit out of it and reopen it doesn't show it. I think that's where the problem is. My xml file is:

<AnchorPane prefHeight="300.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="studentempsignin.MainPage_Controller">
   <children>
      <BorderPane prefHeight="300.0" prefWidth="500.0">
         <center>
            <Text fx:id="empName" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
   </children>
</AnchorPane>

The exception I get is:

SEVERE: null
java.lang.NullPointerException
    at studentempsignin.MainPage_Controller.signingIn(MainPage_Controller.java:190)
    at studentempsignin.SignIn_Controller.lambda$0(SignIn_Controller.java:66)
    at studentempsignin.SignIn_Controller$$Lambda$143/575703892.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)

Edit:

The method empIDAccess is being called when I click the signIn button:

signInBtn.setOnAction((e) -> {
            try {
                //used dbaseDriver...idk why
//                DBaseDriver connect = new DBaseDriver("jdbc:mysql://localhost:3306/AUStudentEmployees",
//                        "kheneahm", "kennygoham");
                String empID = empIDField.getText();
                MainPage_Controller empIDAccess = new MainPage_Controller();
                empIDAccess.signingIn(empID, invalidID, signInBtn);
            } catch (Exception ex) {
                Logger.getLogger(SignIn_Controller.class.getName()).log(Level.SEVERE, null, ex);
            }

        });
James_D
  • 201,275
  • 16
  • 291
  • 322
Bob B
  • 41
  • 4

1 Answers1

0

You're creating a new controller instance. The FXMLLoader creates a controller instance and initializes the @FXML-annotated fields in that instance, when you load the FXML file. Those fields won't be initialized in the new instance you create (and even if they were, they wouldn't refer to the same objects displayed in the UI).

You need to get a reference to the controller that is created when you load the FXML.

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