Ich habe NullPointerException
bekannt verursacht durch Verwendung eines nicht initialisierten Objekts zum Beispiel Button button;
und dann versuchen, es vor button= new Button()
zu verwenden. Aber in diesem Fall kann ich nicht sehen warum und wo bin ich falsch gelaufen.Warum wirft es in diesem Fall NullPointerException?
Ich habe meine Main
Klasse:
public class Main extends Application {
private Stage primaryStage;
private BorderPane mainLayout;
@Override
public void start(Stage primaryStage) throws IOException {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("Employee App");
showMainView();
showMainItems();
}
public void showMainView() throws IOException{
FXMLLoader loader= new FXMLLoader();
loader.setLocation(Main.class.getResource("view/MainView.fxml"));
this.mainLayout=loader.load();
Scene scene= new Scene(this.mainLayout);
this.primaryStage.setScene(scene);
this.primaryStage.show();
}
public void showMainItems() throws IOException{
FXMLLoader loader= new FXMLLoader();
loader.setLocation(Main.class.getResource("view/MainItemsView.fxml"));
BorderPane mainItems=loader.load();
mainLayout.setCenter(mainItems);
}
public void showEletricalScene() throws IOException{
FXMLLoader loader= new FXMLLoader();
loader.setLocation(Main.class.getResource("electrical/ElectricalDeptView.fxml"));
BorderPane eletricalDpet=loader.load();
mainLayout.setCenter(eletricalDpet);
}
public static void main(String[] args) {
launch(args);
}
}
Und mein Controller, wo ich eine Methode in Main
Klasse aufrufen, versuche eine andere Ansicht zu zeigen/laden.
public class MainItemsController {
private Main main;
@FXML private void gotEletrical() throws IOException{
this.main.showEletricalScene();
}
}
Alles wurde gut funktioniert, bis die Zeit, die ich showEletricalScene
nennen versuchen. Ich habe meine Controller gepinnt und alles ist in Ordnung, außer dieser böse Fehler unten:
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)
... 51 more
Caused by: java.lang.NullPointerException
at employee.view.MainItemsController.gotEletrical(MainItemsController.java:11)
... 60 more
Jede Hilfe, danke.
Wo initialisierst du 'main' in' MainItemsController'? –
@James_D So wird es in dem Tutorial gemacht, dem ich folge. – JWizard
Wenn Sie sicher sind, dass das der Fall ist, dann ist das Tutorial falsch. –