0

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:
enter image description here
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>
Mazzone
  • 318
  • 1
  • 4
  • 20
  • Oh, I see the problem. You are using the `static` `FXMLLoader.load(URL))` method, instead of calling a `load` method on the `loader` instance to which the controller is attached. So you need `loader.setLocation(...)` and `loader.load()` instead. (This is also covered many times on SO, don't have time to dig out the duplicates right now.) – James_D Dec 08 '15 at 16:48
  • I'll take a look around, thank you! – Mazzone Dec 08 '15 at 17:04
  • I'm trying, believe me. I just don't understand what all of this is doing, so I'm trying everything I see and it's still giving me an error on Breakout.java line 30, which is paddle.setTranslateX(paddle.getTranslateX() - 4); – Mazzone Dec 08 '15 at 17:12
  • Updated with the full FXML file, and line 30 on Breakout.java is throwing the exception. Also, Breakout.java is the class that's calling Update(). – Mazzone Dec 08 '15 at 17:22
  • I've provided you with every single line of code, sorry I missed that one part... The game engine, and loop (update method), was provided by our professor. It has a method called run() that starts the game loop and runs update(). – Mazzone Dec 08 '15 at 17:27
  • http://cobweb.cs.uga.edu/~mec/fxgame/com/michaelcotterell/game/Game.html – Mazzone Dec 08 '15 at 17:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97311/discussion-between-mazzone-and-james-d). – Mazzone Dec 08 '15 at 17:38

0 Answers0