FXMLLoader导致InvocationTargetException

时间:2018-03-28 19:24:34

标签: java javafx fxml

我刚刚开始使用JavaFX并在尝试加载我的一个FXML文件时遇到问题,即使我在同一个过程中设置了所有这些文件,也就是#34; SinglePlayer.fxml"该程序被假定为Tic-Tac-Toe游戏,并在" coopAction"事件开始了。 这是我的MasterWindow.java:我对这个类的想法是抽象出所有要由这一类控制的控制器。

public class MasterWindow {
protected Stage window;
protected Scene currentScene,loginScene,homeScreen,settingScreen,singleplayerScreen;
protected Parent root,loginSceneParent,homeSceneParent,settingSceneParent,singleplayerSceneParent;
//protected MediaBox mediaBox = new MediaBox();

public MasterWindow(){
    System.out.println("Ceated MasterWindow");
}

public MasterWindow(Stage otherWindow){
    window = otherWindow;

    try {
        connectToLogin();
    } catch (IOException e){e.printStackTrace();}

    window.setTitle("Tic-Tac-Claw");
    window.setMinWidth(450);
    window.setMaxWidth(650);

    window.setMinHeight(500);
    window.setMaxHeight(600);


    window.setWidth(500);
    window.setHeight(550);

}

public void closeProgram(Stage window) {
    boolean result = ExitBox.display("Alert Window", "Do you really want to leave?");
    if (result) {
        System.out.println("Saving files...");
        //mediaBox.getMediaPlayer().stop();
        window.close();
    }
}

protected void connectToSingle() throws IOException {
    singleplayerSceneParent = FXMLLoader.load(getClass().getResource("/singlePlayer.fxml")); 
    //(MasterWindow.java:50) above
    settingScreen = new Scene(singleplayerSceneParent);
    settingScreen.getStylesheets().add("/singlePlayer.css");
}

protected void connectToSetting() throws IOException {
    settingSceneParent = FXMLLoader.load(getClass().getResource("/settingScreen.fxml"));
    settingScreen = new Scene(settingSceneParent);
    settingScreen.getStylesheets().add("/settingScreen.css");
}

protected void connectToHome() throws IOException{
    homeSceneParent = FXMLLoader.load(getClass().getResource("/homeScreen.fxml"));
    homeScreen = new Scene(homeSceneParent);
    homeScreen.getStylesheets().add("/homeScreen.css");
}

protected void connectToLogin() throws IOException{
    loginSceneParent = FXMLLoader.load(getClass().getResource("/loginScreen.fxml"));
    loginScene = new Scene(loginSceneParent);
    loginScene.getStylesheets().add("/login.css");
}

homeScreenController.java:这是打开合作游戏的事件开始的地方

public class homeScreenController extends MasterWindow{

@FXML
private Button coopButton,multiplayerButton,settingButton,exitButton;



public homeScreenController(){
    System.out.println("Created home");
}

public void coopAction(ActionEvent event) throws IOException {
    System.out.println("User press Co-op");
    window = (Stage)((Node)event.getSource()).getScene().getWindow();
    window.setScene(singleplayerScreen);
    this.connectToSingle();               //(homeScreenController.java:40)
    window.show();
}

这是游戏,singlePlayer.java:

public class singlePlayer extends MasterWindow implements Initializable{
...

@FXML
private Pane pane;

public singlePlayer(){
    //this.start();
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    pane.setPrefSize(1000,1000);

    for(int y=0;y< board.length;y++){
        for(int x=0;x<board.length;x++){
            Tile tile = new Tile();
            tile.setTranslateX(x*200);
            tile.setTranslateY(y*200);

            pane.getChildren().add(tile);

            board[x][y] = tile;
        }
    }
    //window.setScene(singleplayerScreen);
    //this.getWindow().setHeight(1000);
    //this.getWindow().setWidth(1000);
    window.show();                      //(singlePlayer.java:52)
}

FXML文件,singlePlayer.fxml

<?import javafx.scene.layout.Pane?>
<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="edu.baylor.ecs.singlePlayer">
</Pane>

堆栈跟踪:

Ceated MasterWindow
User press Login button
Ceated MasterWindow
Created home
User press Co-op
Ceated MasterWindow
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: javafx.fxml.LoadException: 
/K:/IntelliJ/Projects/TicTacClaw/target/classes/singlePlayer.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at edu.baylor.ecs.MasterWindow.connectToSingle(MasterWindow.java:50)
    at edu.baylor.ecs.homeScreenController.coopAction(homeScreenController.java:40)
    ... 58 more
Caused by: java.lang.NullPointerException
    at edu.baylor.ecs.singlePlayer.initialize(singlePlayer.java:52)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 67 more

该项目有一个资源文件夹,里面包含所有FXML和CSS文件,所有其他场景更改都能正常工作。如果您有任何建议或解决方案,任何事情都会有所帮助!

编辑:该行&#34; singleplayerSceneParent = FXMLLoader.load(getClass()。getResource(&#34; /singlePlayer.fxml"));&#34;是什么抛出IOException并且设置与其他FXMLLoader完全一样。其他方法都是偶然的,或者我错过了JavaFX中显而易见的东西。我查看了sample example,我理解NullPointerException是如何发生的,但是在我给它资源之前我不会调用singleplayerSceneParent。

What is a NullPointerException, and how do I fix it?

1 个答案:

答案 0 :(得分:0)

您的异常似乎可能在堆栈跟踪的最后部分:

Caused by: java.lang.NullPointerException
at edu.baylor.ecs.singlePlayer.initialize(singlePlayer.java:52)

singlePlayer 类的初始化方法中,您需要迭代矩阵,直到y< board.lengthx< board.length。其中一个不应该是board.width吗?