Problemer med JavaFX og navigation views imellem.
Hej Eksperten,Jeg har nu kæmpet med ét specielt problem i flere dage, uden succes, så nu prøver jeg her, og krydser fingre for, at der er en venlig sjæl der kan være behjælpelig.
Problemet opstår, når jeg er navigeret fra mit første view til det næste view og dernæst prøver at gå tilbage, altså tilgå det tidligere view. Her får jeg en NullPointerException, og vha. debuggeren kan jeg se, at min controller-klasse påstår, at den reference til main (MainApp) ikke er sat, det bliver den dog hvis jeg debugger min MainApp.
Jeg smider koden til de forskellige klasser og en stacktrace. Jeg har udeladt imports, samt diverse views, da jeg er sikker på, at disse er sat korrekt op. TAK!
MainApp:
public class MainApp extends Application {
private Stage primaryStage;
private AnchorPane overviewPage;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("");
initStartupLayout();
}
public void initStartupLayout() {
try {
FXMLLoader FXMLLoader = new FXMLLoader();
FXMLLoader.setLocation(getClass().getResource("/view/StandardView.fxml"));
overviewPage = (AnchorPane) FXMLLoader.load();
Scene scene = new Scene(overviewPage);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.show();
RootController controller = FXMLLoader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadCreateNewRent() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/CreateNewRentView.fxml"));
AnchorPane page = (AnchorPane) loader.load();
// Create the dialog Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Opret udlejning");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Gets the controller and attaches the stage to display
CreateNewRentController controller = loader.getController();
controller.setDialogStage(dialogStage);
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadAdministrateRentals() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/AdministrateRentalsView.fxml"));
loader.load();
overviewPage.getChildren().set(1, FXMLLoader.load(getClass().getResource("/view/AdministrateRentalsView.fxml")));
AdministrateRentalsController controller = (AdministrateRentalsController) loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public void backToInitView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/AdministrateRentalsView.fxml"));
loader.load();
overviewPage.getChildren().set(1, FXMLLoader.load(getClass().getResource("/view/AdministrateRentalsView.fxml")));
} catch (IOException e) {
e.printStackTrace();
}
}
public void createNewProduct() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/CreateNewRentView.fxml"));
AnchorPane page = (AnchorPane) loader.load();
// Create the dialog Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Opret udlejning");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Gets the controller and attaches the stage to display
CreateNewRentController controller = loader.getController();
controller.setDialogStage(dialogStage);
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
AdministrateRentalsController:
public class AdministrateRentalsController implements Initializable {
private MainApp mainApp;
@FXML
public void backToInitScreen(ActionEvent event) {
mainApp.backToInitView();
}
@FXML
public void createNewProduct(ActionEvent event) {
mainApp.createNewProduct();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
Stacktrace:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 52 more
Caused by: java.lang.NullPointerException
at Controller.AdministrateRentalsController.backToInitScreen(AdministrateRentalsController.java:17)
... 61 more
Råb endelig højt hvis yderligere information er behøvet!