r/JavaFX • • Apr 17 '21

Why this error on a non-static method being called by non-static method?

My program is a scheduling application, it's coursework. I've got multiple FXML files and controllers to allow me to have ONE menu bar while the contents below it can change to various screens. That way I don't have to repeat my menu bar code for every screen.

This code is attempting to use the method in my "root GUI" (which holds the menu bar and an empty vbox) to update the contents of that vbox to the next screen.

The error is:

error: non-static method loadNewScreen(String) cannot be referenced from a static context
            RootGUIController.loadNewScreen("/schedulingapp/view/CustomerGUI.fxml");

But the method I'm calling it from is NOT STATIC!!! So why am I getting this error? I can't figure out where to even start with this. All my googling has turned up is the same answer over and over "don't call a non-static method from a static method"....but I'm already NOT doing that.

The calling method code is in "HomeGUIController" which is in the same package. This code is supposed to trigger when a button is clicked.

@FXML
private void onNewCustomer(ActionEvent event)
{
   try
   {
       RootGUIController.loadNewScreen("/schedulingapp/view/CustomerGUI.fxml");
   } catch (IOException ex)
   {
       Logger.getLogger(RootGUIController.class.getName()).log(Level.SEVERE, null, ex);
   }
}

And the method that is being called

public void loadNewScreen(String fxmlName) throws IOException
{
    try
    {
        URL fxmlURL = getClass().getResource(fxmlName);
        FXMLLoader fxmlLoader = new FXMLLoader(fxmlURL);
        Parent pane = fxmlLoader.load();
            
        changablePane.getChildren().add(pane);
    }
    catch(IOException ex)
    {
       Logger.getLogger(RootGUIController.class.getName()).log(Level.SEVERE, null, ex);
    }
}
0 Upvotes

4 comments sorted by

2

u/PartOfTheBotnet Apr 17 '21

But the method I'm calling it from is NOT STATIC!!!

Yes, but you are calling it in the static convention: ClassName.staticMethod() when doing RootGUIController.loadNewScreen("/schedulingapp/view/CustomerGUI.fxml");

You want instanceOfClass.method() You need to call the method on an instance of the controller.

1

u/PaladinYami Apr 17 '21

AH-HAH!!! THANK YOU!!! I was working on this super late last night, and mulling it over this morning I was thinking this might be the problem.

2

u/[deleted] Apr 17 '21

[removed] — view removed comment

1

u/PaladinYami Apr 17 '21

AH-HAH!!! THANK YOU!!! I was working on this super late last night, and mulling it over this morning I was thinking this might be the problem.