检测击键时的NullPointerException JavaFX - 使用SceneBuilder

时间:2015-12-08 16:23:45

标签: java javafx nullpointerexception runtime-error keystore

我正在使用JavaFX制作一个Breakout游戏,并且我已经在ScenBuilder中创建了场景。我终于让FXML加载器工作,现在当我尝试访问fx:id来操纵我的控制器中的对象(Breakout.java)时,我得到一个空指针异常。

这是控制器代码:

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

}  

以下是FXML代码:
enter image description here
它是一个屏幕截图,因为FXML往往很长,而且我不想炸毁这篇文章。但它显示了我在我尝试访问的内容中添加了id标记的位置。

我注意到的是我在我的Breakout.java类中实例化的Rectangle,paddle说" paddle从未被分配。"我错过了我的控制器无法从我的FXML文件中读取的内容吗?

提前致谢。

编辑:我意识到有类似的问题已被提出,但我无法找到特定于我的解决方案。我对此非常陌生,所以它不知道究竟要找什么,也不能识别不同格式的解决方案。但是我已经尝试了很多不同的连接我的FXML和控制器的方法,没有任何工作,或者我不会发布。请将此标记为副本。

此外,这是我的司机,如果有帮助的话:

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  

这是实际的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>

0 个答案:

没有答案