【问题标题】:Label.setText nullPointerException when switching between scenes [duplicate]在场景之间切换时出现Label.setText nullPointerException [重复]
【发布时间】:2016-03-12 13:01:50
【问题描述】:

我在尝试更改 ActionEvent 上的标签时收到 nullpointerException。

这是困扰我的代码,我在 player1Label.setText(player1.name);线条

我尝试通过编写 player1Label.setText("Eric"); 来手动完成。但仍然得到同样的异常。

Player player1 = new Player();
Player player2 = new Player();


public Label player1Label;
@FXML
public Label player2Label;
@FXML
private TextField player2Field;
@FXML
private TextField player1Field;
@FXML
private Button startButton;

    @FXML
private void startGame(ActionEvent event) throws IOException {

    if (event.getSource() == startButton) {

        player1.name = player1Field.getCharacters().toString();
        player2.name = player2Field.getCharacters().toString();


            player1Label.setText(player1.name);
            player2Label.setText(player2.name);


        Parent Monpoly = FXMLLoader.load(getClass().getResource("FXMLMonopoly.fxml"));
        Scene scene = new Scene(Monpoly);
        Stage stage = (Stage) startButton.getScene().getWindow();
        stage.setScene(scene);
        stage.show();

    }

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

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="528.0" prefWidth="797.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="jope015gop_4.pkg1.FXMLDocumentController">
   <children>
      <ScrollPane fx:id="player1Pane" hbarPolicy="NEVER" layoutX="77.0" layoutY="247.0" prefHeight="200.0" prefWidth="200.0" />
      <ScrollPane fx:id="player2Pane" hbarPolicy="NEVER" layoutX="527.0" layoutY="247.0" prefHeight="200.0" prefWidth="200.0" />
      <Label fx:id="player1Label" layoutX="77.0" layoutY="233.0" text="Player 1" />
      <Label fx:id="player2Label" layoutX="527.0" layoutY="233.0" text="Player 2" />
      <Button id="rollDiceButton" fx:id="rollDiceButton" layoutX="357.0" layoutY="149.0" mnemonicParsing="false" onAction="#nextTurnClicked" prefHeight="43.0" prefWidth="81.0" text="Roll dice" />
      <Label layoutX="319.0" layoutY="55.0" text="MATADOR">
         <font>
            <Font name="System Bold" size="30.0" />
         </font>
      </Label>
   </children>
</AnchorPane>


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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="398.0" prefWidth="671.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="jope015gop_4.pkg1.FXMLDocumentController">
   <children>
      <TextField fx:id="player1Field" layoutX="298.0" layoutY="108.0" />
      <TextField fx:id="player2Field" layoutX="298.0" layoutY="162.0" />
      <Label layoutX="225.0" layoutY="112.0" text="Spiller 1" />
      <Label layoutX="225.0" layoutY="166.0" text="Spiller 2" />
      <Button fx:id="startButton" layoutX="279.0" layoutY="281.0" mnemonicParsing="false" onAction="#startGame" prefHeight="25.0" prefWidth="115.0" text="Start" />
   </children>
</AnchorPane>

我也读过这个:label.setText NullPointerException,但它无法帮助我。

【问题讨论】:

  • 标签是否应该在 FXML 中定义?你能发布FXML文件吗?哪个 FXML 文件?必须有两个,因为您发布的代码显然是一个 FXML 文件的控制器,而您加载了另一个 FXML 文件。
  • 简而言之,这里没有足够的信息来回答这个问题。此代码的上下文是什么(某些 FXML 文件的控制器?哪个?)?标签在哪里定义?
  • 我现在添加了 FXML 文件。这确实是 2 个单独的 FXML 文件的控制器。
  • 不要对多个 FXML 文件的控制器使用相同的类。几乎不可能跟踪控制器类的每个实例中初始化了哪些字段,就像这里的情况一样。
  • 我知道这不是好的做法。只是因为这是一个小项目,我明天要上学。

标签: javafx nullpointerexception label fxml settext


【解决方案1】:

你没有实例化你的标签。你需要这样编码。

player1Label = new Label();

希望这会有所帮助。

【讨论】:

  • 由于到处都有@FXML注释,我认为可以安全地假设标签是在FXML文件中定义的。
猜你喜欢
  • 2016-02-15
  • 2016-09-09
  • 2020-03-23
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 2014-04-28
  • 2017-09-11
  • 1970-01-01
相关资源
最近更新 更多