嘗試更改ActionEvent上的標籤時出現nullpointerException。Label.setText在場景之間切換時發生nullPointerException
這是我的代碼,我在player1Label.setText(player1.name)上獲得nullpointer異常;行
我試圖通過編寫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文件的控制器。 –