call other class method from controller in javafx


Question

I am currently doing an app in JavaFx and in that I am calling main class method from Controller class.I have check sample example provided by Oracle and in that example it's working fine. Here it's my code: Main class:

public class Boiler_New extends Application {

private Stage stage;
@Override
public void start(Stage primarystage) throws Exception {
    stage = primarystage;
    stage.setTitle("Login");
    call1("Login.fxml");
    stage.show();


}
public void call1(String fxml) throws IOException{
    FXMLLoader loader = new FXMLLoader();
    InputStream in = Boiler_New.class.getResourceAsStream(fxml);
    loader.setBuilderFactory(new JavaFXBuilderFactory());
    loader.setLocation(Boiler_New.class.getResource(fxml));
    AnchorPane root = (AnchorPane)loader.load(in);
     Scene scene = new Scene(root);
     stage.setScene(scene);

}
public static void main(String[] args) {
    launch(args);
}
}

Controller:

public class LoginController extends AnchorPane implements Initializable {

private Boiler_New application ;
@FXML
private Label label;

@FXML
private AnchorPane login_panel;

@FXML
private Button login_button;

@FXML
private ImageView close_button;


public void setApp(Boiler_New application){
    this.application = application;
}

@FXML
public void login_act(ActionEvent event) throws Exception {

    application.call1("content.fxml");

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO

}    
}

Tracing:

Executing boiler_new.Boiler_New from C:\Users\BlackWorld\Documents\NetBeansProjects   \Boiler_New\dist\run1175007848\Boiler_New.jar using platform C:\Program Files\Java\jdk1.7.0_25/bin/java
***java.lang.RuntimeException: java.lang.reflect.InvocationTargetException***
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at  com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6863)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:724)
***Caused by: java.lang.reflect.InvocationTargetException***
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
... 44 more
***Caused by: java.lang.NullPointerException***
at boiler_new.LoginController.login_act(LoginController.java:50)
... 54 more
Exception in thread "JavaFX Application Thread" Deleting directory C:\Users\BlackWorld\Documents\NetBeansProjects\Boiler_New\dist\run1175007848

Please help me.

1
2
6/22/2013 3:37:06 PM

I have faced the same problem, to solve it do the following :

  1. Remove "call1("Login.fxml");" from start() method.
  2. Remove the public void call1(String fxml) from Boiler_new.
  3. Add a new function in LoginController as below :

    private void openNewWindow(Event event)
    {
    
     Parent root;
     try {                    
        URL url = getClass().getResource("Content.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(url);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        root = (Parent)fxmlLoader.load(url.openStream());            
        Stage stage = new Stage();
        stage.setScene(new Scene(root, 1000, 800));
        stage.show();
        ((Node)(event.getSource())).getScene().getWindow().hide();
    
      } catch (IOException e) {
          e.printStackTrace();
      }
    }
    
  4. Call this function instead of application.call1("content.fxml");

    @FXML
     public void login_act(ActionEvent event) throws Exception {
    
         openNewWindow(event);
     }
    

    I am sure it works fine.

1
9/20/2013 12:03:42 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon