Javafx Label.setText nullPointerException在场景之间切换时
在尝试更改ActionEvent上的标签时,我遇到nullpointerException 这就是困扰我的代码,我在player1Label.setTextplayer1.name上得到了null指针异常;线条 我试图通过编写player1Label.setTextEric手动完成它;但仍然得到同样的例外Javafx Label.setText nullPointerException在场景之间切换时,javafx,nullpointerexception,label,fxml,settext,Javafx,Nullpointerexception,Label,Fxml,Settext,在尝试更改ActionEvent上的标签时,我遇到nullpointerException 这就是困扰我的代码,我在player1Label.setTextplayer1.name上得到了null指针异常;线条 我试图通过编写player1Label.setTextEric手动完成它;但仍然得到同样的例外 Player player1 = new Player(); Player player2 = new Player(); public Label player1Label; @FXML
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>
我也读过这篇文章:但它没能帮到我。你没有列举你的标签。你需要这样编码
player1Label = new Label();
希望这会有所帮助。标签应该在FXML中定义吗?你能发布FXML文件吗?哪个FXML文件?必须有两个,因为您发布的代码显然是一个FXML文件的控制器,而您加载了另一个FXML文件。对于某些FXML文件,此代码控制器的上下文是什么?哪一个标签是在哪里定义的?我现在添加了FXML文件。这确实是两个独立FXML文件的控制器。不要对多个FXML文件的控制器使用同一类。事实上,跟踪控制器类的每个实例中初始化了哪些字段是不可能的,这里就是这样。我知道这不是一个好的做法。我之所以这么做,是因为这是一个小项目,我明天要去上学。因为到处都有@FXML注释,我认为可以安全地假设标签是在FXML文件中定义的。