【问题标题】:label.setText NullPointerExceptionlabel.setText NullPointerException
【发布时间】:2014-04-28 01:39:11
【问题描述】:

你好,第一次来这里:

我有一个 JavaFX 应用程序,它可以动态更改 FXML UI 标签,并且数据是从 Player 类中提取的。

有问题的两个类是Player.javaInterfaceHandler.java

播放器类存储播放器详细信息,我想将详细信息传递给设置标签上的文本的接口类。

作为测试,我的 FXML UI 只有一个按钮和两个标签。

如果单击按钮,它会调用handleButton 方法,它将locationLabel 设置为“Town”。

但是,如果我在 Player 类中调用 locationLabel() 方法,则在调用 nameLabel.setText(name) 时会收到 NullPointerException。通过调试发现Interface类中的name字符串应该是“Dan”。

谁能帮忙?

主类:

public class Main extends Application {

    public void start(final Stage mainStage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
        Scene scene = new Scene(root);
        mainStage.setTitle("Main Screen");
        mainStage.setScene(scene);
        mainStage.show();    
    }

public static void main(String[] args) {
        launch(args);
    }
}

播放器类:

public class Player{

InterfaceHandler ui = new InterfaceHandler();   

 public void setNameLabel() {

    String name = "Dan";
    ui.setName(name);
}

InterfaceHandler 类:

    public class InterfaceHandler implements Initializable {

       public Label nameLabel;
       public Label locationLabel;    

public void handleButton(ActionEvent event) throws IOException {        

        locationLabel.setText("Town");
    }

public void setName(String name){       
        nameLabel.setText(name);
    }    
}

MainScreen.fxml:

 <?xml version="1.0" encoding="UTF-8"?>

    <?import java.lang.*?>
    <?import java.net.*?>
    <?import java.util.*?>
    <?import javafx.scene.*?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.effect.*?>
    <?import javafx.scene.image.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.text.*?>

    <AnchorPane id="AnchorPane" prefHeight="629.0" prefWidth="600.0" snapToPixel="true" style="-fx-background-color:  beige;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.InterfaceHandler">
      <children>            
        <Button fx:id="button1" layoutX="512.0" layoutY="381.0" minWidth="14.0" mnemonicParsing="false" onAction="#handleButton" prefHeight="30.0" prefWidth="51.0" text="Town" visible="true" />        

        <Label fx:id="nameLabel" layoutX="57.0" layoutY="8.0" prefWidth="216.0" text="blank" />
        <Label fx:id="locationLabel" layoutX="68.0" layoutY="27.0" prefWidth="193.0" text="blank" />
      </children>
    </AnchorPane>

【问题讨论】:

    标签: java nullpointerexception javafx fxml settext


    【解决方案1】:

    这是因为您没有正确地从 FXML 文件中注入您的 Labels

    注释您的 Label 变量。带FXML注解:

    public class InterfaceHandler implements Initializable {
        @FXML
        public Label nameLabel;
        @FXML
        public Label locationLabel;    
    
        public void handleButton(ActionEvent event) throws IOException {        
            locationLabel.setText("Town");
        }
    
        public void setName(String name){       
            nameLabel.setText(name);
        }    
    }
    

    此外,InterfaceHandler 是您在 FXML 中使用 fx:controller 引用的控制器。这指示 FXMLLoader 在加载程序加载其 FXML 时创建 InterfaceHandler 的新实例。所以不要在你的 Player 类中创建新的 InterfaceHandler,而是将 InterfaceHandler 作为 Player 的构造函数参数,并使用 loader.getController 从 FXMLLoader 中获取 InterfaceHandler 控制器实例。

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("MainScreen.fxml"));
    Parent root = (Parent)loader.load();
    Player player = new Player(loader.getController());
    Scene scene = new Scene(root);
    
    . . .
    
    public class Player {
      private InterfaceHandler ui;   
    
      public Player(InterfaceHandler ui) {
        this.ui = ui;
      }
    
      public void setNameLabel() {
        String name = "Dan";
        ui.setName(name);
      }
    }
    

    【讨论】:

    • 感谢这工作!但是我确实必须添加一个演员才能使其工作:Player player = new Player((InterfaceHandler) loader.getController());
    • 如果您使用 NetBeans,请使用 make controller 选项从 fxml 创建控制器文件。它将为您进行 FXML 注入。
    【解决方案2】:

    我遇到了类似的问题,但结果证明我已将标签设为静态属性,当我删除静态部分时,它修复了空指针异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2015-01-30
      • 2021-04-24
      相关资源
      最近更新 更多