0

I encounter this problem at several places in my application. 1) I have one Window with a textfield and a button. On clicking, a function is executed that uses the input of the textfield. Everything works till here. 2) I link the button instead to a 2nd window that opens when clicking. In the 2nd window I create a button that executes the same function as in in 1 ), still utilizing the Textfield in the 1st window. I get a java.lang.NullPointerException. Both windows are still open and are linked to the same controller class.

What's the issue here and how can I solve it? Thank you for your help!

   @FXML
        private TextField DeleteIdText;
    @FXML
        private Button DeleteOrderBtn;
    @FXML
        private Button DeleteYesBtn;
//This is the code I use to open the 2nd window:
    @FXML
        public void openDeleteConfirmation(){
            try {
                Stage primaryStage = new Stage();
                Parent root = FXMLLoader.load(getClass().getResource("/Menu/ConfirmDeleteView.fxml"));
                Scene scene = new Scene(root);
                primaryStage.setScene(scene);
                primaryStage.show();

            } catch (IOException e) {
                System.out.println("exception" + e);
                e.printStackTrace();
            }

// This is the function I try to execute:

    @FXML
        public void deleteDish(ActionEvent event) throws SQLException, ClassNotFoundException {
            // TODO Autogenerated
            try {
                MenuDAO.deleteDish(DeleteIdText.getText());
            } catch (SQLException e) {
                System.out.println("Error occurred while deleting item \n" + e);
                throw e;


    }
  • How/where are the Buttons/Textfields declared? Could you please post a bit more code of your controller and the `DeleteIdText` (class/field declaration?)? It seems to me like you do something in static while you dont want to. If you followed the java naming conventions, `DeleteIdTest` is a class from which you call a method (`getText()`) from in static context. So what were you expecting if that is the case? – n247s Dec 12 '16 at 22:16
  • 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) – VGR Dec 12 '16 at 22:27
  • Thanks for your reply. You are right I didn't follow the naming convention here, sorry for the confusion. I added the textfield and button declaration. The rest is just the usual FXML file, created with Scene Builder. Not sure about the static, do you mean that I need to instantiate the textfield somehow? – Franklin2603 Dec 12 '16 at 22:29
  • After reading that, it looks like DeleteIdText.getText() is null, once the window is not in focus anymore. Only way I see is to store the value in another variable before going to the next window, but this is probably not an elegant way? – Franklin2603 Dec 12 '16 at 22:46
  • "Both windows are ... linked to the same controller class." Do you mean you are using the same class for controllers for two different FXML files? Don't do that- it is too difficult to keep track of what is initialised and what is not in each of the different controller instances. Use a different controller class for each FXML file. – James_D Dec 13 '16 at 00:41

0 Answers0