java – label.setText NullPointerException

嗨第一次来这里但是这里:

我有一个JavaFX应用程序,它动态地更改FXML UI标签,并从Player类中提取数据.

有问题的两个类是Player.java和InterfaceHandler.java.

播放器类存储播放器详细信息,我想将详细信息传递给Interface类,后者在标签上设置文本.

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

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

但是,如果我在我的Player类中调用locationLabel()方法,则在调用nameLabel.setText(name)时会出现NullPointerException.通过调试,我发现Interface类中的名称字符串应该是“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>

解决方法:

这是因为您没有从FXML文件中正确注入标签.

标注您的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);
  }
}
上一篇:Swing / Java:如何正确使用getText和setText字符串


下一篇:个人项目(WC.exe)(java)(基于图形界面)