NullPointerException load fxml

后端 未结 2 1930
忘掉有多难
忘掉有多难 2021-01-28 01:03

I want to load an fxml file in my application. I use the next code:

try {
   FXMLLoader loader = new FXMLLoader();
   loader.setController(null);
   loader.setRo         


        
相关标签:
2条回答
  • 2021-01-28 01:27

    The proper syntax for loading fxml is:

    FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));
    

    The reason for your NullPointerException is likely due to the fact that I don't see any definition of your fxml variable. Also, I'm not sure about that .openStream() bit at the end of yours.

    see here for more info on FXML.

    EDIT

    In light of the comments, There are..3 or so places there I see that you could be getting this NPE.

    Place #1) loader is null, since we can see it's clearly not, this is a non-issue. place #2) JfxUtils is a non-existing item, but based on the error, this is not it either. Place #3 the getResource(fxml) is returning the null because it either points to something that doesn't exist, thus returning null, and you can't open a stream on a null object.

    this line: at javafx.fxml.FXMLLoader.isCyclic(FXMLLoader.java:1868) in the exception stands out to me, although I don't have time right this second to dive in and explore it. Something in my gut tells me the cause of some of the weirdness might be tied into it.

    0 讨论(0)
  • 2021-01-28 01:49

    Here is the solution.

    Add this line for the loader:

    loader.setLocation(JfxUtils.class.getResource(fxml));
    

    Very bad bug.

    0 讨论(0)
提交回复
热议问题