JavaFX任务中的NPE

时间:2015-05-27 10:27:11

标签: javafx javafx-2 javafx-8

我实现了这个代码示例,以便从数据库加载数据:

...........
    Tab tabdata = new Tab();
                            Task<VBox> task = createLoadImagesTask(data);
                            StackPane stack = createVeiledPane(task, 120, 120);
                            runTask(task);
                            tabdata.setContent(stack);
                            AgentsDataStage.addNewTab(tabdata, newValue.getValue().toString());
    ............


private static Task<VBox> createLoadImagesTask(final InternalData data)
    {
        return new Task<VBox>()
        {
            @Override
            protected VBox call() throws Exception
            {

                    TabContent contentc = new TabContent();
                    return contentc.initTestTabContentData();

            }
        };
    }

    private static void runTask(Task<? extends Node> task)
    {
        Thread taskThread = new Thread(task, "Task Runner");
        taskThread.setDaemon(true);
        taskThread.start();
    }

    private static StackPane createVeiledPane(Task<? extends Node> task, double prefWidth, double prefHeight)
    {
        Region veil = new Region();
        veil.setStyle("-fx-background-color: transparent");
        veil.setPrefSize(prefWidth, prefHeight);
        ProgressIndicator progress = new ProgressIndicator();
        double progressSize = Math.min(prefWidth, prefHeight);
        clampSize(progress, progressSize * 0.75);
        progress.progressProperty().bind(task.progressProperty());
        StackPane stack = new StackPane();
        stack.getChildren().setAll(veil, progress);

        task.setOnSucceeded(new EventHandler<WorkerStateEvent>()
        {
            @Override
            public void handle(WorkerStateEvent t)
            {
                stack.getChildren().setAll(task.getValue());
            }
        });

        return stack;
    }

    private static void clampSize(Control control, double size)
    {
        control.setMinSize(ProgressIndicator.USE_PREF_SIZE, ProgressIndicator.USE_PREF_SIZE);
        control.setPrefSize(size, size);
        control.setMaxSize(ProgressIndicator.USE_PREF_SIZE, ProgressIndicator.USE_PREF_SIZE);
    }

当我运行代码并在TreeView节点上多次单击我启动代码时,我得到了这个堆栈异常:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
        at com.sun.javafx.css.StyleManager.findMatchingStyles(Unknown Source)
        at javafx.scene.CssStyleHelper.createStyleHelper(Unknown Source)
        at javafx.scene.Node.reapplyCss(Unknown Source)
        at javafx.scene.Node.impl_reapplyCSS(Unknown Source)
        at javafx.scene.Node.invalidatedScenes(Unknown Source)
        at javafx.scene.Node.setScenes(Unknown Source)
        at javafx.scene.Parent$1.onChanged(Unknown Source)
        at com.sun.javafx.collections.TrackableObservableList.lambda$new$19(Unknown Source)
        at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(Unknown Sourc
e)
        at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(Unknown Source)
        at javafx.collections.ObservableListBase.fireChange(Unknown Source)
        at javafx.collections.ListChangeBuilder.commit(Unknown Source)
        at javafx.collections.ListChangeBuilder.endChange(Unknown Source)
        at javafx.collections.ObservableListBase.endChange(Unknown Source)
        at javafx.collections.ModifiableObservableListBase.add(Unknown Source)
        at java.util.AbstractList.add(Unknown Source)
        at com.sun.javafx.collections.VetoableListDecorator.add(Unknown Source)
        at com.sun.javafx.scene.control.skin.ProgressIndicatorSkin$IndeterminateSpinner.rebuild(Unkn
own Source)
        at com.sun.javafx.scene.control.skin.ProgressIndicatorSkin$IndeterminateSpinner.access$400(U
nknown Source)
        at com.sun.javafx.scene.control.skin.ProgressIndicatorSkin$2.invalidated(Unknown Source)
        at javafx.beans.property.IntegerPropertyBase.markInvalid(Unknown Source)
        at javafx.beans.property.IntegerPropertyBase.set(Unknown Source)
        at javafx.css.StyleableIntegerProperty.set(Unknown Source)
        at javafx.beans.property.IntegerProperty.setValue(Unknown Source)
        at javafx.css.StyleableIntegerProperty.applyStyle(Unknown Source)
        at javafx.css.StyleableIntegerProperty.applyStyle(Unknown Source)
        at javafx.scene.CssStyleHelper.transitionToState(Unknown Source)
        at javafx.scene.Node.impl_processCSS(Unknown Source)
        at javafx.scene.Parent.impl_processCSS(Unknown Source)
        at javafx.scene.control.Control.impl_processCSS(Unknown Source)
        at javafx.scene.Parent.impl_processCSS(Unknown Source)
        at javafx.scene.Parent.impl_processCSS(Unknown Source)
        at javafx.scene.Node.processCSS(Unknown Source)
        at javafx.scene.Node.processCSS(Unknown Source)
        at javafx.scene.Node.processCSS(Unknown Source)
        at javafx.scene.Node.processCSS(Unknown Source)
        at javafx.scene.Node.processCSS(Unknown Source)
        at javafx.scene.Node.processCSS(Unknown Source)
        at javafx.scene.Scene.doCSSPass(Unknown Source)
        at javafx.scene.Scene.access$3600(Unknown Source)
        at javafx.scene.Scene$ScenePulseListener.pulse(Unknown Source)
        at com.sun.javafx.tk.Toolkit.lambda$runPulse$31(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.Toolkit.runPulse(Unknown Source)
        at com.sun.javafx.tk.Toolkit.firePulse(Unknown Source)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
        at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$401(Unknown Source)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$146(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

您能告诉导致此异常的原因吗? 这可能与线程有关吗?

更新

与此代码相同的结果:

public void addNewTab(DynamicTreeNodeModel nodeModel)
    {
        final Tab tab = new Tab();

        final ProgressBar progressBar = new ProgressBar();
        final Task<BorderPane> loadDataTask = new Task<BorderPane>()
        {
            @Override
            public BorderPane call() throws Exception
            {
                Content content = new Content(primaryStage, treeView.getSelectionModel().getSelectedItem());
                return content.initContentData();
            }
        };
        tab.setContent(progressBar);
        loadDataTask.setOnSucceeded(new EventHandler<WorkerStateEvent>()
        {
            @Override
            public void handle(WorkerStateEvent event)
            {
                BorderPane data = loadDataTask.getValue();
                tab.setContent(data);
            }
        });
        final Thread thread = new Thread(loadDataTask);
        thread.setDaemon(true);
        thread.start();

        newTabLabel = new Label(nodeModel.getName());
        tab.setStyle("-fx-focus-color: transparent;");  // Remove the blue dashed label from label
        tab.setGraphic(newTabLabel);
        tab.setContextMenu(makeTabContextMenu(tab, newTabLabel, tabPane));

        tabPane.getTabs().add(0, tab);  
        tabPane.getSelectionModel().select(tab);  
        tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);  
    }

0 个答案:

没有答案