@FXMLViewFlowContext空指针异常

时间:2016-11-13 08:18:04

标签: java javafx flow-control viewflow

我想为我的客户端创建一个JavaFX应用程序。我找到了DataFX框架并且喜欢它。但我对@FXMLViewFlowContext有疑问,因为每当我从父母那里调用子视频时它会返回null。我使用了lib 8.0.7及以下代码

  1. 我使用i18n资源在主控制器中创建侧视图:

    // side controller will add links to the content flow
    Flow sideMenuFlow = new Flow(SideMenuController.class);
    sideMenuFlowHandler = new FlowHandler(sideMenuFlow, context, Common.getViewConfiguration());
    drawer.setSidePane(sideMenuFlowHandler.start(new AnimatedFlowContainer(Duration.millis(320), ContainerAnimations.SWIPE_LEFT)));
    
  2. 这是我的侧窗格控制器

    @FXMLController(value = "/res/fxml/test.fxml")
    public class SideMenuController {
        @FXMLViewFlowContext
        private ViewFlowContext context;
    
    
        @FXML
        private JFXListView test;
    
    
        @FXML
        @ActionTrigger("suppliers")
        private JFXButton suppliers;
    
    
        @FXML
        @ActionTrigger("history")
        private JFXButton history;
    
    
        @FXML
        @ActionTrigger("barcode")
        private JFXButton barcode;
    
    
        @FXML
        @ActionTrigger("settings")
        private JFXButton settings;
    
    
        @FXML
        @ActionTrigger("report")
        private JFXButton report;
    
    
        @FXML
        @ActionTrigger("users")
        private JFXButton users;
    
    
        @FXML
        @ActionTrigger("inventory")
        private JFXButton inventory;
    
    
        @FXML
        @ActionTrigger("shops")
        private JFXButton shops;
    
    
        @FXML
        @ActionTrigger("calculation")
        private JFXButton calculation;
    
    
        @PostConstruct
        public void initialize(){
            test.propagateMouseEventsToParent();
            System.out.println(context);
    
    
            FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
            Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");
            bindNodeToController(suppliers, SuppliersController.class, contentFlow, contentFlowHandler);
            bindNodeToController(inventory, InventoryController.class, contentFlow, contentFlowHandler);
            bindNodeToController(users, UsersController.class, contentFlow, contentFlowHandler);
            bindNodeToController(history, HistoryController.class, contentFlow, contentFlowHandler);
            bindNodeToController(shops, ShopsController.class, contentFlow, contentFlowHandler);
            bindNodeToController(report, ReportController.class, contentFlow, contentFlowHandler);
            bindNodeToController(barcode, BarcodeController.class, contentFlow, contentFlowHandler);
            bindNodeToController(calculation, CalculationController.class, contentFlow, contentFlowHandler);
            bindNodeToController(settings, SettingsController.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();
                }
            });
        }
    }
    
  3. 当我运行我的程序时,我得到NullPointerException,因为从不注入上下文。我搜索但没有找到任何解决方案。我也尝试旧图书馆,但没有帮助。

1 个答案:

答案 0 :(得分:2)

最后我解决了我的问题。这个问题有两个原因。 1.第一个与 DataFX 库版本相关。 2.第二个问题是初始化 mehtod。当 DataFX 开始创建控制器类实例时,初始化方法将由javafx FXMLLoader.load()方法调用,这在init ViewFlowContext ,以便我得到 NullPointerException 。换句话说,如果您使用 @PostConstruct 注释并使用名称 initialize 调用您的方法,就像我一样,您的方法将被调用两次,即创建新控制器类实例时(上下文还没有init),另一个是当 @PostConstruct 方法被调用时(上下文已经创建)。这对于 DataFX flow lib 8.0b8 版本是正确的。我查看了这个版本 lib 8.0.7 ,但我又得到了 NullPointerException 。所以我改变了我的lib版本,现在一切正常