I'm making a Breakout game using JavaFX and I've made the scene in ScenBuilder. I finally got the FXML loader to work, and now when I try to access the fx:id to manipulate objects in my controller (Breakout.java), I get a null pointer exception.
Here's the controller code:
public class Breakout extends Game{
/**
* Constructs the game.
* @param stage the primary stage
*/
public Breakout(Stage stage) {
super(stage, "Breakout", 60, 414, 550);
} // TestGame
@FXML private Rectangle paddle;
@FXML private Text score_label;
@FXML private Text lives_label;
@Override
public void update(Game game, GameTime gameTime) {
//score_label.setText("Score: 1337"); //TODO
//lives_label.setText("Lives: 3"); //TODO
if (game.getKeyManager().isKeyPressed(KeyCode.LEFT))
paddle.setTranslateX(paddle.getTranslateX() - 4);
if (game.getKeyManager().isKeyPressed(KeyCode.RIGHT))
paddle.setTranslateX(paddle.getTranslateX() + 4);
} // update
}
And here's the FXML code:
It's a screenshot because FXML tends to get REALLY long, and I didn't want to blow up this post. But it shows where I added an id tag to what I'm trying to access.
Something I noticed is that my instantiated Rectangle, paddle, in my Breakout.java class is saying "paddle is never assigned." So am I missing something where my controller isn't able to read from my FXML file?
Thanks in advance.
EDIT: I realize similar questions have been asked, but I cannot find a solution specific to mine. I'm very new to this, so it's a combo of not knowing what exactly to look for, and not recognizing a solution in a different format. But I've tried many different ways of connecting my FXML and controller, nothing has worked yet or I wouldn't have posted. Please unmark this as a duplicate.
Also, here's my Driver, in case that's helpful:
public class Driver extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Game game = new Breakout(primaryStage);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Breakout.class.getResource("BreakoutFXML.fxml"));
Pane myPane = FXMLLoader.load(getClass().getResource("BreakoutFXML.fxml"));
game.getScene().setRoot(myPane);
primaryStage.setTitle(game.getTitle());
primaryStage.setScene(game.getScene());
primaryStage.show();
game.run();
} // start
public static void main(String[] args) {
launch(args);
} // main
} // Driver
Here's the actual FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0"
prefWidth="414.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Text fx:id="score_label" layoutX="15.0" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="SCORE:"/>
<Text fx:id="lives_label" layoutX="350.0" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="LIVES:"/>
<Text fx:id="score_val" layoutX="60.0" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="0"/>
<Text fx:id="lives_val" layoutX="390.0" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="3"/>
<Group>
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f25135" height="45.0" layoutX="276.0" layoutY="48.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f25135" height="45.0" layoutY="48.0" stroke="BLACK"
strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f25135" height="45.0" layoutX="207.0" layoutY="48.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f25135" height="45.0" layoutX="138.0" layoutY="48.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f25135" height="45.0" layoutX="69.0" layoutY="48.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f25135" height="45.0" layoutX="345.0" layoutY="48.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
</children>
</Group>
<Group>
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#eb9e33" height="45.0" layoutX="345.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#eb9e33" height="45.0" layoutY="93.0" stroke="BLACK"
strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#eb9e33" height="45.0" layoutX="138.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#eb9e33" height="45.0" layoutX="207.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#eb9e33" height="45.0" layoutX="69.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#eb9e33" height="45.0" layoutX="276.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
</children>
</Group>
<Group layoutY="45.0">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e4e43a" height="45.0" layoutX="345.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e4e43a" height="45.0" layoutY="93.0" stroke="BLACK"
strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e4e43a" height="45.0" layoutX="138.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e4e43a" height="45.0" layoutX="207.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e4e43a" height="45.0" layoutX="69.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e4e43a" height="45.0" layoutX="276.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
</children>
</Group>
<Group layoutY="90.0">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#37b55f" height="45.0" layoutX="345.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#37b55f" height="45.0" layoutY="93.0" stroke="BLACK"
strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#37b55f" height="45.0" layoutX="138.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#37b55f" height="45.0" layoutX="207.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#37b55f" height="45.0" layoutX="69.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#37b55f" height="45.0" layoutX="276.0" layoutY="93.0"
stroke="BLACK" strokeType="INSIDE" width="68.75"/>
</children>
</Group>
<Rectangle fx:id="paddle" arcHeight="5.0" arcWidth="5.0" fill="#818b94" height="25.0" layoutX="107.0"
layoutY="511.0" stroke="#818b94" strokeType="INSIDE" width="200.0"/>
<Circle fx:id="ball" layoutX="207.0" layoutY="498.0" radius="12.5" stroke="BLACK" strokeType="INSIDE"/>
</children>
</Pane>