2013-06-22 4 views
2

Je suis en train de faire une application en JavaFx et en ce que j'appelle la méthode de la classe principale de la classe Controller.J'ai un exemple d'exemple de vérification fourni par Oracle et dans cet exemple cela fonctionne bien. Ici, il est mon code: Classe principale:appeler autre méthode de classe du contrôleur dans javafx

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 

S'il vous plaît aidez-moi.

+0

Aller dans le débogage et vérifier la valeur de retour de Boiler_New.class.getResourceAsStream (FXML) à regarder si elle est nulle –

+0

Je suis le solution.Thanks.Error est contrôleur est pas initialisé lorsque l'application est lancée. – Sarju

Répondre

1

J'ai fait face au même problème, de le résoudre procédez comme suit:

  1. Supprimer "Call1 (" Login.fxml ");" de la méthode start().
  2. Supprimez l'appel public vo1 (String fxml) de Boiler_new.
  3. Ajouter une nouvelle fonction dans LoginController comme ci-dessous:

    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. appeler cette fonction au lieu de application.call1 ("content.fxml");

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

    Je suis sûr que cela fonctionne très bien.

Questions connexes