使用LAF Nimbus在Java Native Classes,SynthTreeUI中发现NullPointerException的原因

时间:2018-05-27 03:24:16

标签: java swing nullpointerexception look-and-feel nimbus

评论者注意:这可能是NimbusLookAndFeel中的一个(可能是已知的)错误,并且(目前)还不是规范NPE问题的副本。

我有多个类,但我不知道Exception可能暗示哪些类。

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.plaf.synth.SynthTreeUI.paint(SynthTreeUI.java:382)
        at javax.swing.plaf.synth.SynthTreeUI.update(SynthTreeUI.java:269)
        at javax.swing.JComponent.paintComponent(JComponent.java:780)
        at javax.swing.JComponent.paint(JComponent.java:1056)
        at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
        at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
        at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
        at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
        at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
        at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
        at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
        at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
        at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

如您所见,该异常不会将我的代码链接到任何地方。

create a Minimal, Complete, and Verifiable example 我无法通过我的所有项目,因为它很长,我不能在其上放一段代码,因为我没有发现源的线索。

我延伸:

  1. DefaultMutableTreeNode开始

    @Override public String toString()
    
  2. DefaultTreeCellRenderer开始

    @Override public Component getTreeCellRendererComponent(JTree tree, 
        Object value, boolean selected, boolean expanded, boolean leaf, 
        int row, boolean hasFocus)
    
  3. 现在的情况是:

    //Fails
    //javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
    
    //Works
    //javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    
    //Works
    //javax.swing.UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
    

    注意: 我不需要解决方案。如果不提供代码,这将是不合逻辑的!

    问题: 我应该在哪里找到问题的根源?

1 个答案:

答案 0 :(得分:0)

我与JTree的情况类似,我的问题的原因是(在Thread中更新JTree),救助是使用SwingWorker。 解释基于与

类似的帖子

https://www.javacodegeeks.com/2012/12/multi-threading-in-java-swing-with-swingworker.html

我不假装提供严格的功能性解决方案或代码,我只是向您介绍如何解决问题。

class SwingWorkerCustomDTO extends SwingWorker<Void, CustomDTO> {

    @Override
    protected Void doInBackground() throws Exception {
        //Initialization and Iterative Operation over JTree
        myJTree = new JTree(/**/); // or Set the Model

        //someLongTask(arguments);
        /*long time/delayed Task that he sometimes performs o operations on JTree.*/

        return null;
    }

    @Override
    protected void done() {
        //reload or update JTree /*or what you imagine doing*/
        //removeOrUpdateTask();

        //example
        DefaultTreeModel treeModel = ((DefaultTreeModel)myJTree.getModel());
        DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
        treeModel.reload(rootTreeNode);
    }

    @Override
    protected void process(List<CustomDTO> chunks) {
        for (final CustomDTO chunk : chunks) {
            //The Operation (Add/Remove/Update) of JTree are performed here!.
            switch (chunk.getOperarionType()) {
              case ADD_CONSTANT:  /*or what you define*/
                //Add Node(Object or Objects[]) to Some JTree;
                break;
              case UPDATE_CONSTANT:  /*or what you define*/
                //Update Node(Object or Objects[]) to Some JTree;
                break;
              case REMOVE_CONSTANT:
                //Remove Node(Object or Objects[]) from Some JTree;
                break;
            }
        }
    }

    public void callPublish(CustomDTO customDTO) {
        /*I expose a public method to pass arguments that make functionalities
        (Add/Remove/Update Node) required in the JTree invoked externally.*/
        publish(customDTO);
    }

}

您可以在外部调用自己的自定义类

swingWorkerCustomDTO.callPublish(
    new CustomDTO(int OperationType/*or what you define*/,
        Object argument /*or Object[] arguments*/));

CustomDTO是由您定义的类,在其中包含int属性之类的include操作(您想做什么),以及所需的另一个属性。