0

I have known NullPointerException to be caused by using an uninitialized object for example Button button;and then try to use it before button= new Button(). However, in this case I cant see why and where have I gone wrong.

I have my Main class:

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);
}
}

And my controller where I am trying to call a method in Main class to show/load another view.

public class MainItemsController {
private Main main;
@FXML private void gotEletrical() throws IOException{
    this.main.showEletricalScene();
    }

  }

Everything has been working fine until the time I am trying to call showEletricalScene. I have pinned my controllers and everything is fine except this nasty error below:

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

Any help, thanks.

CN1002
  • 1,115
  • 3
  • 20
  • 40

1 Answers1

0

You have defined

private Main main;

and then try calling

this.main.showEletricalScene();

before you have ever assigned a value to main, hence, null.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • I am following a tutorial and it is exactly done that way. But `Main` object isn't created the time I run my `Main` class? – CN1002 Mar 21 '16 at 13:54