尝试更改ActionEvent上的标签时,我收到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但它无法帮助我。
答案 0 :(得分:-2)
您没有标记您的标签。你需要像这样编码。
player1Label = new Label();
希望这会有所帮助。