代码之家  ›  专栏  ›  技术社区  ›  kevin caradant

为什么我使用io.datafx获得java.lang.NullPointerException?

  •  1
  • kevin caradant  · 技术社区  · 8 年前

    我发现了JAVAFX,并实现了对项目结构的重构 一个小时后,也许这很愚蠢,但我真的不理解这个问题。 我的项目现在正在maven中(我不知道这是否会改变什么) 这是我的错误:

    GRAVE: null
    io.datafx.controller.flow.FlowException: io.datafx.controller.FxmlLoadException: java.lang.reflect.InvocationTargetException
        at io.datafx.controller.flow.FlowHandler.start(FlowHandler.java:143)
        at io.datafx.controller.flow.FlowHandler.start(FlowHandler.java:101)
        at maxiRename.controller.MainController.initialize(MainController.java:88)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        at io.datafx.controller.ViewFactory.createByController(ViewFactory.java:161)
        at io.datafx.controller.flow.FlowHandler.start(FlowHandler.java:140)
        at maxiRenameApp.MainMaxiFilm.start(MainMaxiFilm.java:27)
        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)
    Caused by: io.datafx.controller.FxmlLoadException: java.lang.reflect.InvocationTargetException
        at io.datafx.controller.ViewFactory.createByController(ViewFactory.java:180)
        at io.datafx.controller.flow.FlowHandler.start(FlowHandler.java:140)
        ... 17 more
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at io.datafx.controller.ViewFactory.createByController(ViewFactory.java:175)
        ... 18 more
    Caused by: java.lang.NullPointerException
        at maxiRename.controller.SideMenuController.init(SideMenuController.java:65)
        ... 23 more
    

    这是我的侧栏MenuController.java:

    package maxiRename.controller;
    
    import javax.annotation.PostConstruct;
    
    import com.jfoenix.controls.JFXListView;
    
    import io.datafx.controller.FXMLController;
    import io.datafx.controller.flow.Flow;
    import io.datafx.controller.flow.FlowException;
    import io.datafx.controller.flow.FlowHandler;
    import io.datafx.controller.flow.context.FXMLViewFlowContext;
    import io.datafx.controller.flow.context.ViewFlowContext;
    import io.datafx.controller.util.VetoException;
    import javafx.fxml.FXML;
    import javafx.scene.Node;
    import javafx.scene.control.Label;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    
    @FXMLController(value = "/maxiRename/view/SideMenu.fxml", title = "Material Design Example")
    public class SideMenuController {
    
    
        @FXML
        private StackPane root;
    
        @FXML
        private VBox listViewDrawer;
    
        @FXML
        private ImageView imageTopDrawer;
    
        @FXML
        private JFXListView<?> list1;
    
        @FXML
        private Label home;
    
        @FXML
        private JFXListView<?> subList;
    
        @FXML
        private Label musics;
    
        @FXML
        private Label movies;
    
        @FXML
        private Label series;
    
        @FXML
        private Label help;
    
        @FXML
        private Label exit;
    
        @FXMLViewFlowContext
        private ViewFlowContext context;
    
        @PostConstruct
        public void init() throws FlowException, VetoException {
            list1.propagateMouseEventsToParent();
            System.out.println(context);
            FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
            Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");
            System.out.println(subList.getItems().get(0));
            bindNodeToController(home, HomeController.class, contentFlow, contentFlowHandler);
            bindNodeToController(musics, MusicsSettingsController.class, contentFlow, contentFlowHandler);
            bindNodeToController(movies, MoviesSettingsController.class, contentFlow, contentFlowHandler);
            bindNodeToController(series, SeriesSettingsController.class, contentFlow, contentFlowHandler);
            bindNodeToController(help, HelpController.class, contentFlow, contentFlowHandler);
        }
    
        private void bindNodeToController(Node node, Class<?> controllerClass, Flow flow, FlowHandler flowHandler) {
            flow.withGlobalLink(node.getId(), controllerClass);
            node.setOnMouseClicked((e) -> {
                try {               
                    flowHandler.handle(node.getId());               
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        }
    }
    

    我的结构文件,如果可以帮助您: Structure project

    我的第一个想法是在一条不好的路径上或类似的东西,但如果我改变它,我会得到错误“不设置位置”,所以我想我的路径是好的。

    @FXMLController(value = "/maxiRename/view/SideMenu.fxml", title = "Material Design Example")
    

    编辑:maxiRename.controller.SideMenuController.init(SideMenu controller.java:65)是:

    FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
    and context is null
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   kevin caradant    8 年前

    好吧,我找到了!

    这是一个名为“flow”的库。在版本8.0中,它可以工作,但在8.1中无法工作。

    我从maven那里得到了这个:

    <dependency>
        <groupId>io.datafx</groupId>
        <artifactId>flow</artifactId>
        <version>8.1</version>
    </dependency>
    

    现在我有了这个:

    <dependency>
            <groupId>io.datafx</groupId>
            <artifactId>flow</artifactId>
            <version>8.0</version>
        </dependency>
    

    谢谢,我希望这对其他人有用;)