Discover Reason of NullPointerException in Java Native Classes, SynthTreeUI using LAF Nimbus

二次信任 提交于 2019-12-11 17:39:17

问题



Note to reviewers: This may be a (possibly known) bug in NimbusLookAndFeel, and is not (as yet) a dup to the canonical NPE question.


I have multiple classes, but I have no clue which of them the Exception might imply.

    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)

As you can see, the exception does not link my code anywhere.

For create a Minimal, Complete, and Verifiable example I can not pass all my project because it is very long, and I can not put a piece of code on it because I do not have a clue to discover the source.

I extending:

  1. from DefaultMutableTreeNode in order to

    @Override public String toString()
    
  2. from DefaultTreeCellRenderer in order to

    @Override public Component getTreeCellRendererComponent(JTree tree, 
        Object value, boolean selected, boolean expanded, boolean leaf, 
        int row, boolean hasFocus)
    

Now the situation is:

//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");

NOTE: I'm not requiring a solution. That would be illogical, without supplying code!!!

The Question: Where specifically should I look for the root of my problem?


回答1:


I have similar situation with JTree, the reason of my problem was (updating JTree inside of Thread), the salvation was using SwingWorker. The explanation was based on similar post to

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

I do not pretend to give a strictly functional solution or code, I'm just giving you an idea of how to solve the problem.

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);
    }

}

Externally you can to call your own Custom Class

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

CustomDTO is a class defined by you, where include operation (what do you want to do), like int property, and another property needed.



来源:https://stackoverflow.com/questions/50548764/discover-reason-of-nullpointerexception-in-java-native-classes-synthtreeui-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!