1

I am getting a nullpointerException when trying to change my labels on an ActionEvent.

This is the code that is bugging me, i am getting nullpointer exception on the player1Label.setText(player1.name); lines

I've tried to do it manually by writing player1Label.setText("Eric"); But still get the same exception.

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>

I have also read this one: label.setText NullPointerException but it hasn't been able to help me out.

Community
  • 1
  • 1
  • Are the labels supposed to be defined in FXML? Can you post the FXML file? And which FXML file? There must be two, since the code you posted is clearly the controller for one FXML file, and you load another FXML file. – James_D Dec 07 '15 at 19:55
  • In short, there's nowhere near enough information here to answer the question. What is the context for this code (controller for some FXML file? which?)? Where are the labels defined? – James_D Dec 07 '15 at 20:08
  • I added the FXML file now. This is indeed the controller of 2 seperate FXML files. – Jonas Mohr Pedersen Dec 07 '15 at 20:10
  • Don't use the same class for the controllers for multiple FXML files. It becomes virtually impossible to keep track of which fields are initialized in each of the instances of the controller class, as is the case here. – James_D Dec 07 '15 at 20:14
  • I am aware that this is not good practice. Just did it because it is a small project that i'm turning in tomorrow for school. – Jonas Mohr Pedersen Dec 07 '15 at 20:16
  • The reason it's not good practice is because it's so difficult to make it work... Create a different controller class for each FXML. – James_D Dec 07 '15 at 20:18

1 Answers1

-2

You didn't instance your label. You need to code it like this.

player1Label = new Label();

Hope this will help.

grahan
  • 2,148
  • 5
  • 29
  • 43
  • 1
    Since there are `@FXML` annotations all over the place, I think it's safe to assume that the labels are defined in the FXML file. – James_D Dec 07 '15 at 19:57