java.lang.NullPointerException in handling with controls between 2 controllers - Javafx

前端 未结 1 1761
悲&欢浪女
悲&欢浪女 2021-01-28 18:30

I\'ve been trying button events between 2 scenes in Javafx.

This is Start.fxml (in view package):



        
相关标签:
1条回答
  • 2021-01-28 19:11

    OK so the NPE is being thrown on this line:

        lbl1.setText(PreferencesController.txt2.getText());
    

    At the basic Java level, are only two ways that you could get that to happen:

    • when lnl1 is null, or
    • when PreferencesController.txt2 is null.

    No other explanations are possible ... given that PreferencesController.txt2 is a static field.


    I'm not familiar with JavaFX or FXML, but I suspect that your mistake is declaring the fields as static. I suspect that this causes the FXML mechanisms to not inject the reference to the UI component correctly. So those variables remain as null ... and you get an NPE.

    Confirmation is here: https://stackoverflow.com/a/23109125/139985. (Thanks for the link @James_D)

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