【问题标题】:JavaFX ColorPicker NullPointerExceptionJavaFX ColorPicker NullPointerException
【发布时间】:2017-03-15 14:21:52
【问题描述】:

当我点击自定义颜色链接时,我得到了这个可怕的 NullPointerException:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.stage.Window$9.invalidated(Window.java:867)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
at javafx.stage.Window.setShowing(Window.java:922)
at javafx.stage.Window.show(Window.java:937)
at javafx.stage.Stage.show(Stage.java:259)
at com.sun.javafx.scene.control.skin.CustomColorDialog.show(CustomColorDialog.java:170)
at com.sun.javafx.scene.control.skin.ColorPalette$1.handle(ColorPalette.java:127)
at com.sun.javafx.scene.control.skin.ColorPalette$1.handle(ColorPalette.java:105)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Hyperlink.fire(Hyperlink.java:153)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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)

外观:

有没有可能修复它?也许有一些方法可以切断底部的链接?代码:

colorMenu = new Menu("Color");
    ColorPicker colorPicker = new ColorPicker();
    colorPicker.setValue(Color.FIREBRICK);
    m_palette = new MenuItem(null, colorPicker);
    m_palette.setOnAction(event -> gameScene.setCellColor(colorPicker.getValue()));

感谢您的任何建议!

【问题讨论】:

  • 不是“什么是空指针异常”的副本,因为堆栈跟踪不包含客户端代码。
  • 你使用的是哪个 JDK 版本?
  • 看起来很相似stackoverflow.com/questions/26768955/…。他们建议更新 Java。
  • 是的,无论您如何运行它,它看起来都非常糟糕。在我的 Mac 上,对于
  • 我使用的是 1.8.0_101 版本。我会更新JDK,看看是否有帮助。

标签: java javafx java-8


【解决方案1】:

我通过在 MenuItem 中创建 Menu 对象来避免这个问题。现在看起来像这样:Screenshot

【讨论】:

    【解决方案2】:

    可以从 ColorPicker 中删除有问题的超链接。 在这里,我们用我们自己的 ColorPickerSkin 制作了一个 ColorPicker。然后我们可以拦截 在皮肤返回之前在皮肤中创建弹出内容以删除超链接。

    public class CustomColorPicker extends ColorPicker {
    
        @Override
        protected Skin<?> createDefaultSkin() {
            return new CustomColorPickerSkin(this);
        }
    
        private class CustomColorPickerSkin extends ColorPickerSkin {
    
            public CustomColorPickerSkin(ColorPicker control) {
                super(control);
            }
    
            @Override
            protected Node getPopupContent() {
                Node colorPalette = super.getPopupContent(); // This is an instance of private API ColorPalette which
                                                                // extends Region
                Region r = (Region) colorPalette;
                List<Node> vboxes = r.getChildrenUnmodifiable().stream().filter(e -> {
                    return e instanceof VBox;
                }).collect(Collectors.toList()); // This ColorPalette contains a VBox which contains the Hyperlink we want
                                                    // to remove.
                for (Node n : vboxes) {
                    VBox vbox = (VBox) n;
                    List<Node> hyperlinks = vbox.getChildren().stream().filter(e -> {
                        return e instanceof Hyperlink;
                    }).collect(Collectors.toList());
                    vbox.getChildren().removeAll(hyperlinks); // Remove the hyperlink
                }
                return colorPalette;
            }
    
        }
    
    }
    

    这将在 ColorPalette 中的任何 VBox 中查找并删除任何超链接。请记住,如果 ColorPalette 的结构发生变化,它会崩溃,这是完全可能的,因为它不是公共 API。

    外观:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 2016-09-04
      • 2020-12-18
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多