Reputation:
Yeah i know this question has often been asked, i used the searching function but couldl'nt solve my problem with these answers, and now im tired to search at google or anywhere else.
My Problem: I try to load an FXML File into my Javacode:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
//Layout laden
Pane pane = FXMLLoader.load(getClass().getResource("layout/main_layout.fxml"));
//Scene erstellen und initialisieren
Scene scene = new Scene(pane);
scene.getStylesheets().add(getClass().getResource("css/main_layout.css").toExternalForm());
primaryStage.setScene(scene);
//Breite und Höhe der Stage setzen
primaryStage.setWidth(1024);
primaryStage.setHeight(768);
primaryStage.setTitle("Untitled - jNotepad");
primaryStage.show();
} catch(IOException e) {
e.printStackTrace();
}
}
}
My FXML file is in the package de.toxiclab.jNotepad.layout, that means realtive to the path in layout/main_layout.fxml, so the path has to be correct.
And then it prints me this ugly exception:
javafx.fxml.LoadException: /J:/Eclipse%20Workspace/jNotepad/bin/de/toxiclab/jNotepad/layout/main_layout.fxml:19
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3191) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3164) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3140) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3120) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3113) at de.toxiclab.jNotepad.Main.start(Main.java:32) at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837) at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335) at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301) at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39) at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112) at java.lang.Thread.run(Thread.java:744) Caused by: java.lang.NullPointerException at de.toxiclab.jNotepad.NotepadController.getStage(NotepadController.java:199) at de.toxiclab.jNotepad.NotepadController.(NotepadController.java:45) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at java.lang.Class.newInstance(Class.java:433) at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932) at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:976) at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216) at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:738) at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 19 more
But i dont understand, why theres an damn Nullpointerexception, the PATH is 100% correct.
Upvotes: 0
Views: 1704
Reputation: 1919
So after seeing the error seemingly centering around the controller class 'NotePadController' I believe the reason you are seeing this NullPointerException
is due to the fact that the nodes haven't been attached to the scene yet. In light of this, you may want to try attaching a WindowEvent.WINDOW_SHOWN
handler onto that point in the method.
for example:
you mentioned that line 199 was return ((Stage)this.textPane.getScene().getWindow());
perhaps, instead, it would be better to have a static Window
variable (say called 'window' ) and say:
(Stage)this.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
window = ((Stage)this.textPane.getScene().getWindow());
}
});
or something along those lines.
It's also possible the main meat of the error is coming from somewhere else. Good luck! sometimes these types of problems can be a touch tricky.
Upvotes: 2