2017-08-30 55 views
0

时正如标题建议试图从视图中分离的逻辑的时候,我只是想分开(组织)我的项目里面的文件,我读过的一切在这之前:位置错误使用JavaFX

  1. How to seperate Javafx(non fxml) to a fxml and a controller? (Invocation target exception)
  2. Is there a way to modularize a JavaFX application?
  3. How to seperate logic from controller JavaFx

这是最接近的答案。

  • How to separate my gui in javafx?
  • 但它没有对我来说很清楚仍然寿,(也许这是关于我无法理解语法的Cuz的英语不是我的母语郎)。

    我试图手动复制最后一个,但仍然无法确定我应该如何指向fxml文件。

    这里是我的结构:

    My project Structure

    我主要的应用程序的代码:

    import javafx.application.Application; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.Parent; 
    import javafx.scene.Scene; 
    import javafx.scene.image.Image; 
    import javafx.stage.Stage; 
    /** 
    * Principal 
    * @author Adrian 
    * 
    */ 
    public class Login extends Application { 
    
        @Override 
        public void start(Stage primaryStage) { 
         try { 
          Parent root = FXMLLoader.load(getClass().getResource("login.fxml")); 
          Scene scene = new Scene(root,300,200); 
          scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
          primaryStage.setTitle("Login"); 
          primaryStage.setScene(scene); 
          primaryStage.show(); 
          Image icono = new Image(getClass().getResourceAsStream("/SiLi/src/main/recursos/img/usuario.gif")); 
          primaryStage.getIcons().add(icono); 
         } catch(Exception e) { 
          e.printStackTrace(); 
         } 
        } 
    
        public static void main(String[] args) { 
         launch(args); 
        } 
    } 
    

    的错误,我得到:

    > java.lang.NullPointerException: Location is required. 
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
        at org.ajfmo.sili.core.Login.start(Login.java:19) 
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
        at java.security.AccessController.doPrivileged(Native Method) 
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
        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.lambda$null$148(WinApplication.java:191) 
        at java.lang.Thread.run(Thread.java:745) 
    
    +0

    你可以找到越来越多的Q/A就像你想要的。对于你的错误,你应该验证你的fxml文件的路径。 –

    +0

    [使用JavaFx应用MVC]的可能副本(https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) –

    +0

    请参阅https://stackoverflow.com/questions/35630415/nullpointer-location-on -javafx-fxmlloader和https://stackoverflow.com/questions/34828952/jafafx-load-fxml-file-error-null-pointer-exception –

    回答

    0

    我解决了这个问题如下因素James_D评论,仍然不清楚为什么会发生这种情况(我猜是这样与绝对和相对路径以及当前工作目录有关)。无论如何,我设法将我的项目组织到正确的文件夹中。

    使用由詹姆斯提供的信息:

    当然,它应该是的getClass()的getResource( “/ FXML/login.fxml”)。?不知道异常与MVC有什么关系,你只是有错误的路径到FXML文件。 - James_D

    ,并在正确的路径或文件Login.fxml我“Login.java”级控制器的位置指示,试图指向控制器的包装容器是这样的:

    fx:controller="org.ajfmo.sili.controlador.CLogin" 
    

    允许我来解决这个问题,并保持在轨道...

    谢谢你帮我。