Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javafx、scenebuilder中移动ImageView时出现NullPointerException_Javafx_Nullpointerexception_Imageview_Scenebuilder - Fatal编程技术网

在Javafx、scenebuilder中移动ImageView时出现NullPointerException

在Javafx、scenebuilder中移动ImageView时出现NullPointerException,javafx,nullpointerexception,imageview,scenebuilder,Javafx,Nullpointerexception,Imageview,Scenebuilder,我知道以前在Stackoverflow上也有过类似的问题,但我找到的答案似乎都不能解决我的问题。我还想补充一点,我对javafx完全陌生,一周前就开始编写java代码了,所以我为代码中的任何愚蠢错误道歉! 当尝试在我的javafx中移动ImageView,在游戏“sokoban”中移动玩家和板条箱时,我得到一个nullpointerexception,指出我的ImageView(在场景生成器中用fx:id标记,然后在我的控制器类中调用)是一个空对象。这是我的密码: 应用程序: import ja

我知道以前在Stackoverflow上也有过类似的问题,但我找到的答案似乎都不能解决我的问题。我还想补充一点,我对javafx完全陌生,一周前就开始编写java代码了,所以我为代码中的任何愚蠢错误道歉! 当尝试在我的javafx中移动ImageView,在游戏“sokoban”中移动玩家和板条箱时,我得到一个nullpointerexception,指出我的ImageView(在场景生成器中用fx:id标记,然后在我的控制器类中调用)是一个空对象。这是我的密码: 应用程序:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class App extends Application{

    @Override
    public void start(final Stage primaryStage) throws Exception {
        primaryStage.setTitle("Sokoban");
        primaryStage.setScene(new Scene(FXMLLoader.load(App.class.getResource("StartScreen.fxml"))));
        primaryStage.show();

    }

    public static void main(final String[] args) {
        App.launch(args);
    }
}
控制器:

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class SokobanController {

    GameState currentGame;

    int stepSize;

    @FXML Button Easy;
    @FXML Button Medium;
    @FXML Button Hard;
    @FXML AnchorPane easyGameScene;
    @FXML AnchorPane mediumGameScene;
    @FXML AnchorPane hardGameScene;
    @FXML ImageView playerEasy;
    @FXML ImageView crateEasy0;
    @FXML ImageView crateEasy1;
    @FXML ImageView crateEasy2;
    @FXML ImageView crateEasy3;

    List<ImageView> crateImagesEasy = 

Arrays.asList(crateEasy0,crateEasy1,crateEasy2,crateEasy3);



    @FXML void handleDifficultySelection(ActionEvent event) throws IOException {
        String difficulty = ((Button) event.getSource()).getId();
        System.out.println(difficulty);
        if (difficulty.equals("Easy")) {
            stepSize = 75;
        }
        else if (difficulty.equals("Medium")) {

        }
        else if (difficulty.equals("Hard")) {

        }
        currentGame = new GameState(difficulty);

//      playerEasy = new ImageView(new Image("@player.png")); 

        Scene gameScene = new Scene(FXMLLoader.load(getClass().getResource("GameScreen.fxml")));
        gameScene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                handleOnKeyPressed(event);
            }
        });
        Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();

        window.setScene(gameScene);
        window.show();
        }

    @FXML void handleOnkey(ActionEvent event) {
        System.out.println("hallo");
    }

    void handleOnKeyPressed(KeyEvent event) {
        System.out.println("hallo");
        if (event.getCode().equals(KeyCode.LEFT)) {
//          Translate translate = new Translate();
//          translate.setX(-stepSize);
            if (!currentGame.checkCollideWall(-1, 0)) {
                int i = 0;
                for (Crate crate : currentGame.crates) {
                    if (currentGame.checkCollideCrate(crate, -1, 0)
                            && !currentGame.checkCollideWall(-2, 0)) {
//                      crateImagesEasy.get(currentGame.crates.indexOf(crate))
//                          .getTransforms().addAll(translate);


crateImagesEasy.get(currentGame.crates.indexOf(crate)).relocate(-stepSize,0);
//                      playerEasy.getTransforms().addAll(translate);
                        i = 1;
                        playerEasy.relocate(-stepSize, 0);
                    }
                }
                if (i == 0) {
//                  playerEasy.getTransforms().addAll(translate);
                    playerEasy.relocate(-stepSize, 0);
                }
            }   
        }
        else if (event.getCode().equals(KeyCode.RIGHT)) {
            Translate translate = new Translate();
            translate.setX(stepSize);
            if (!currentGame.checkCollideWall(1, 0)) {
                int i = 0;
                for (Crate crate : currentGame.crates) {
                    if (currentGame.checkCollideCrate(crate, 1, 0)
                            && !currentGame.checkCollideWall(2, 0)) {
                        crateImagesEasy.get(currentGame.crates.indexOf(crate))
                            .getTransforms().addAll(translate);
                        playerEasy.getTransforms().addAll(translate);
                        i = 1;
                    }
                }
                if (i == 0) {
                    playerEasy.getTransforms().addAll(translate);
                }
            }   
        }
        else if (event.getCode().equals(KeyCode.UP)) {
            Translate translate = new Translate();
            translate.setY(-stepSize);
            if (!currentGame.checkCollideWall(0, -1)) {
                int i = 0;
                for (Crate crate : currentGame.crates) {
                    if (currentGame.checkCollideCrate(crate, 0, -1)
                            && !currentGame.checkCollideWall(0, -2)) {
                        crateImagesEasy.get(currentGame.crates.indexOf(crate))
                            .getTransforms().addAll(translate);
                        playerEasy.getTransforms().addAll(translate);
                        i = 1;
                    }
                }
                if (i == 0) {
                    playerEasy.getTransforms().addAll(translate);
                }
            }   
        }
        else if (event.getCode().equals(KeyCode.DOWN)) {
            Translate translate = new Translate();
            translate.setY(stepSize);
            if (!currentGame.checkCollideWall(0, 1)) {
                int i = 0;
                for (Crate crate : currentGame.crates) {
                    if (currentGame.checkCollideCrate(crate, 0, 1)
                            && !currentGame.checkCollideWall(0, 2)) {
                        crateImagesEasy.get(currentGame.crates.indexOf(crate))
                            .getTransforms().addAll(translate);
                        playerEasy.getTransforms().addAll(translate);
                        i = 1;
                    }
                }
                if (i == 0) {
                    playerEasy.getTransforms().addAll(translate);
                }
            }   
        }

    }
}

(第97行是我尝试调用ImageView对象playerEasy上的方法的第一点)

这是否回答了您的问题?我不这么认为。ImageView对象不应该已经通过将其fx:id链接到控制器类的scene builder FXML文件初始化了吗?我可能错了。可能必须在控制器类中创建ImageView对象,然后在显示场景之前将其添加到场景中,但在场景生成器中构建场景时如何执行此操作?注释是自动创建的,忘记删除它:)这是重复的,因为其他问题的答案会引导您找到位置(你似乎已经知道这一点)以及它发生的原因:从那开始,开始实验:首先把你的例子剥离成a(记住M,它不需要大量的图像/控件,只需要使其可复制),然后修改以找到它发生的时间和不发生的时间-宾果:)这是否回答了你的问题?我不这么认为。ImageView对象不应该已经通过将其fx:id链接到控制器类的scene builder FXML文件初始化了吗?我可能错了。可能必须在控制器类中创建ImageView对象,然后在显示场景之前将其添加到场景中,但在场景生成器中构建场景时如何执行此操作?注释是自动创建的,忘记删除它:)这是重复的,因为其他问题的答案会引导您找到位置(你似乎已经知道了这一点)以及它发生的原因:从那开始,开始实验:首先将你的示例剥离为a(记住M,它不需要大量的图像/控件,只需要使其具有可复制性),然后修改以找到它发生的时间和不发生的时间-宾果:)
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane prefHeight="630.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.SokobanController">
   <children>
      <VBox prefHeight="630.0" prefWidth="600.0">
         <children>
            <StackPane prefHeight="600.0" prefWidth="600.0">
               <children>
                  <AnchorPane fx:id="easyGameScene" prefHeight="600.0" prefWidth="600.0" style="-fx-background-color: f3e1bb;">
                     <children>
                        <ImageView fitHeight="75.0" fitWidth="75.0" pickOnBounds="true" translateX="150.0">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="150.0" layoutY="150.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="150.0" layoutY="225.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutY="225.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutY="300.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutY="375.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="75.0" layoutY="225.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="150.0" layoutY="75.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="225.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="300.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="300.0" layoutY="75.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="300.0" layoutY="150.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="375.0" layoutY="150.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="450.0" layoutY="150.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="525.0" layoutY="150.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="525.0" layoutY="225.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="525.0" layoutY="300.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="450.0" layoutY="300.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="375.0" layoutY="300.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="375.0" layoutY="375.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="375.0" layoutY="450.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="375.0" layoutY="525.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="300.0" layoutY="525.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="225.0" layoutY="525.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="225.0" layoutY="450.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="225.0" layoutY="375.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="150.0" layoutY="375.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fitHeight="75.0" fitWidth="75.0" layoutX="75.0" layoutY="375.0" pickOnBounds="true">
                           <image>
                              <Image url="@wall.PNG" />
                           </image>
                        </ImageView>
                        <ImageView fx:id="crateEasy0" fitHeight="75.0" fitWidth="75.0" layoutX="225.0" layoutY="225.0" pickOnBounds="true">
                           <image>
                              <Image url="@crate.png" />
                           </image>
                        </ImageView>
                        <ImageView fx:id="crateEasy2" fitHeight="75.0" fitWidth="75.0" layoutX="225.0" layoutY="300.0" pickOnBounds="true">
                           <image>
                              <Image url="@crate.png" />
                           </image>
                        </ImageView>
                        <ImageView fx:id="crateEasy3" fitHeight="75.0" fitWidth="75.0" layoutX="300.0" layoutY="375.0" pickOnBounds="true">
                           <image>
                              <Image url="@crate.png" />
                           </image>
                        </ImageView>
                        <ImageView fx:id="crateEasy1" fitHeight="75.0" fitWidth="75.0" layoutX="375.0" layoutY="225.0" pickOnBounds="true">
                           <image>
                              <Image url="@crate.png" />
                           </image>
                        </ImageView>
                        <ImageView fx:id="playerEasy" fitHeight="75.0" fitWidth="75.0" layoutX="300.0" layoutY="300.0" pickOnBounds="true">
                           <image>
                              <Image url="@player.png" />
                           </image>
                        </ImageView>
                        <Circle fill="#eb6020" layoutX="113.0" layoutY="338.0" radius="15.0" stroke="BLACK" strokeType="INSIDE" />
                        <Circle fill="#eb6020" layoutX="263.0" layoutY="113.0" radius="15.0" stroke="BLACK" strokeType="INSIDE" />
                        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f5f5f500" height="75.0" layoutX="75.0" layoutY="300.0" stroke="#eb6020" strokeType="INSIDE" strokeWidth="5.0" width="75.0" />
                        <Circle fill="#eb6020" layoutX="488.0" layoutY="263.0" radius="15.0" stroke="BLACK" strokeType="INSIDE" />
                        <Circle fill="#eb6020" layoutX="338.0" layoutY="488.0" radius="15.0" stroke="BLACK" strokeType="INSIDE" />
                        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f5f5f500" height="75.0" layoutX="225.0" layoutY="75.0" stroke="#eb6020" strokeType="INSIDE" strokeWidth="5.0" width="75.0" />
                        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f5f5f500" height="75.0" layoutX="450.0" layoutY="225.0" stroke="#eb6020" strokeType="INSIDE" strokeWidth="5.0" width="75.0" />
                        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#f5f5f500" height="75.0" layoutX="300.0" layoutY="451.0" stroke="#eb6020" strokeType="INSIDE" strokeWidth="5.0" width="75.0" />
                     </children>
                  </AnchorPane>
               </children>
            </StackPane>
            <HBox alignment="CENTER" prefHeight="30.0" prefWidth="600.0" style="-fx-background-color: #000000;">
               <children>
                  <Text fill="#d79420" strokeType="OUTSIDE" strokeWidth="0.0" text="Controls: move with arrow-keys and press &quot;R&quot; to restart the game">
                     <font>
                        <Font name="System Bold" size="15.0" />
                     </font>
                  </Text>
               </children>
            </HBox>
         </children>
      </VBox>
   </children>
</AnchorPane>
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at ovinger/app.SokobanController.handleOnKeyPressed(SokobanController.java:97)
    at ovinger/app.SokobanController$1.handle(SokobanController.java:64)
    at ovinger/app.SokobanController$1.handle(SokobanController.java:1)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$KeyHandler.process(Scene.java:4058)
    at javafx.graphics/javafx.scene.Scene$KeyHandler.access$1500(Scene.java:4004)
    at javafx.graphics/javafx.scene.Scene.processKeyEvent(Scene.java:2121)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2595)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$1(GlassViewEventHandler.java:248)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
    at javafx.graphics/com.sun.glass.ui.View.handleKeyEvent(View.java:547)
    at javafx.graphics/com.sun.glass.ui.View.notifyKey(View.java:971)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:830)