I've just started using JavaFX and having an issue when trying to load one of my FXML files, "SinglePlayer.fxml", even though I set them all up in the same process. This program is suppose to be a Tic-Tac-Toe game and loads when the "coopAction" event is started. This is my MasterWindow.java: my idea for this class was to abstract all of the controllers to be controlled by this one class.
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: This is where the event to open the coop game starts
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();
}
This is the game, 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)
}
The FXML file, 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>
The stack trace:
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
The project has a resources folder with all of the FXML and CSS files inside and all of the other scene changes work correctly. If you have any advice or solutions, anything would help!
Edit: The line " singleplayerSceneParent = FXMLLoader.load(getClass().getResource("/singlePlayer.fxml"));" is what throws the IOException and is setup exactly like the other FXMLLoaders. Either the other methods worked by chance or I'm missing something obvious with JavaFX. I've looked at What is a NullPointerException, and how do I fix it? and I understand how a NullPointerException occurs but I dont call the singleplayerSceneParent until I give it the resource.