Text.setText() not updating text, throws NullPointerException

好久不见. 提交于 2019-12-12 03:38:34

问题


I'm having a bit of trouble with the Text.setText() method. It is supposed to set the String name as it's value.

The FXML code has the text:

              <Text fx:id="airlineName" strokeType="OUTSIDE" strokeWidth="0.0" text="Airline Name Goes Here" GridPane.rowIndex="1">
                 <font>
                    <Font size="17.0" />
                 </font>
              </Text>

Here is a fragment of the java code:

 @FXML
 Text airlineName;

 private void loadNewWindow(String resource, Button button) throws IOException {
        Stage stage = (Stage) button.getScene().getWindow();
        Parent root = FXMLLoader.load(getClass().getResource(resource));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

 @FXML
 public void viewAirline() throws IOException {
        loadNewWindow("/main/resources/Airline Page.fxml", viewAirline);
        //some code here, involves Object selected
        String name = selected.name;
        airlineName.setText(name); //This is line 67
        airlineName.setFill(Color.RED);
        System.out.println(airlineName.getText());

    }

The error message:

Caused by: java.lang.reflect.InvocationTargetException
    ...
javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 45 more
Caused by: java.lang.NullPointerException
at main.GUI.Controller.viewAirline(Controller.java:67)

The viewAirline method was not used in java, but in FXML:

<Button fx:id="viewAirline" mnemonicParsing="false" onAction="#viewAirline" text="View Airline" GridPane.columnIndex="1" />

It is supposed to be activated whenever a button (similarly named) viewAirline is pressed.

This FXML code is a different FXML code referenced in the viewAirline() method ("/main/resources/Airline Page.fxml"). The Text element is referenced in the Airline Page.fxml. All FXML files use the same controller.


回答1:


If you specify the controller class in the fxml file using the fx:controller attribute, the FXMLLoader creates a new instance of the controller during the loading. This leads to the airlineName being null since that field is injected to a different instance of the controller class. To use the same controller instance, you need to set the controller before loading the fxml file and also remove the fx:controller attribute from the fxml file:

FXMLLoader loader = new FXMLLoader(fxmlURL);
loader.setController(controllerInstance);
Parent root = loader.load();

However I wouldn't recommend using the same controller for multiple fxmls. This leads to a high coupling and changes in the controller or one fxml files may break something in the other fxmls. Especially the initialize method would need to take all fxmls into account.

You better use different controllers and make them communicate. You can even program against an interface to avoid relying on a concrete controller class.

You can get the controller instance created by FXMLLoader (fx:controller attribute present) after calling load() using getController():

FXMLLoader loader = new FXMLLoader(fxmlURL);
Parent root = loader.load();
ControllerClass controller = loader.getController();



回答2:


It might be throwing null pointer exception may be because of the reason that the name attribute in your selected.name is null i.e it is not assigned any value.




回答3:


You're not instantiating the airlineName object. Try the code below:

airlineName = new Text();
airlineName.setText(name);



回答4:


Maybe its not recognizing the airLine text tag, because the tag is not closed try to close your text tag:

<Text fx:id="airlineName" strokeType="OUTSIDE" strokeWidth="0.0" text="Airline Name Goes Here" GridPane.rowIndex="1" />

OR

 <Text fx:id="airlineName" strokeType="OUTSIDE" strokeWidth="0.0" text="Airline Name Goes Here" GridPane.rowIndex="1"></Text>


来源:https://stackoverflow.com/questions/38099098/text-settext-not-updating-text-throws-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!