与隐藏的 Swing GUI 元素相关的 NullPointerException

NullPointerException related to hidden Swing GUI elements

https://github.com/carlb710/myProgram

我正在使用 IntelliJ Swing GUI 编辑器制作程序的前端。根据微调器返回的 int 值,显示或隐藏 GUI 的某些元素。每次我更改该 int 值并重新启动程序时,都会抛出许多异常之一,所有 NullPointer 异常。我认为他们总是在同一个线程中,这只是不同的原因。

错误通常如下所示:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot read field "top" because "topInsets" is null
    at java.desktop/com.apple.laf.AquaTabbedPaneCopyFromBasicUI.rotateInsets(AquaTabbedPaneCopyFromBasicUI.java:2000)
    at java.desktop/com.apple.laf.AquaTabbedPaneCopyFromBasicUI.getTabAreaInsets(AquaTabbedPaneCopyFromBasicUI.java:1681)
    at java.desktop/com.apple.laf.AquaTabbedPaneUI$AquaTruncatingTabbedPaneLayout.superCalculateTabRects(AquaTabbedPaneUI.java:1133)
    at java.desktop/com.apple.laf.AquaTabbedPaneUI$AquaTruncatingTabbedPaneLayout.calculateTabRects(AquaTabbedPaneUI.java:1091)
    at java.desktop/com.apple.laf.AquaTabbedPaneCopyFromBasicUI$TabbedPaneLayout.calculateLayoutInfo(AquaTabbedPaneCopyFromBasicUI.java:2322)
    at java.desktop/com.apple.laf.AquaTabbedPaneCopyFromBasicUI$TabbedPaneLayout.layoutContainer(AquaTabbedPaneCopyFromBasicUI.java:2226)
    at java.desktop/java.awt.Container.layout(Container.java:1538)
    at java.desktop/java.awt.Container.doLayout(Container.java:1527)
    at java.desktop/java.awt.Container.validateTree(Container.java:1723)
    at java.desktop/java.awt.Container.validate(Container.java:1658)
    at java.desktop/com.apple.laf.AquaTabbedPaneCopyFromBasicUI.ensureCurrentLayout(AquaTabbedPaneCopyFromBasicUI.java:1335)
    at java.desktop/com.apple.laf.AquaTabbedPaneUI.paint(AquaTabbedPaneUI.java:148)
    at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
    at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5271)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060)
    at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:75)
    at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:112)
    at java.desktop/java.awt.Container.paint(Container.java:2003)
    at java.desktop/java.awt.Window.paint(Window.java:3949)
    at java.desktop/javax.swing.RepaintManager.run(RepaintManager.java:876)
    at java.desktop/javax.swing.RepaintManager.run(RepaintManager.java:848)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

当我 运行 在我的主 class 中使用以下代码为我的程序创建 JFrame 实例时,似乎会抛出异常:

 App window = null;
    try{
        window = new App();
    } catch(NullPointerException e){
        e.printStackTrace();
    }


    JFrame frame = new JFrame("Java QA Checker");
    frame.setContentPane(window.landing);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    Font font = new Font("Helvetica", Font.PLAIN, 16);

我的 GUI 构造函数看起来像这样,我认为问题出在这里:

//listener for spinner, changes number of elements on screen
        spinner1.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                numOfFiles = (Integer) spinner1.getValue();

//switch statement sets number of elements for file settings visible or not based on spinner value up to 12
                try {
                    switch(numOfFiles){
                        case 0:{
                            file1OriginLabel.setVisible(true);
                            file2OriginLabel.setVisible(true);
                            file3OriginLabel.setVisible(true);
                            file4OriginLabel.setVisible(true);
                            file5OriginLabel.setVisible(true);
                            file6OriginLabel.setVisible(true);
                            file7OriginLabel.setVisible(true);
                            file8OriginLabel.setVisible(true);
                            file9OriginLabel.setVisible(true);
                            file10OriginLabel.setVisible(true);
                            file11OriginLabel.setVisible(true);
                            file12OriginLabel.setVisible(true);

                            file1OriginInputted.setVisible(true);
                            file2OriginInputted.setVisible(true);
                            file3OriginInputted.setVisible(true);
                            file4OriginInputted.setVisible(true);
                            file5OriginInputted.setVisible(true);
                            file6OriginInputted.setVisible(true);
                            file7OriginInputted.setVisible(true);
                            file8OriginInputted.setVisible(true);
                            file9OriginInputted.setVisible(true);
                            file10OriginInputted.setVisible(true);
                            file11OriginInputted.setVisible(true);
                            file12OriginInputted.setVisible(true);

                            addFileButton1.setVisible(true);
                            addFileButton2.setVisible(true);
                            addFileButton3.setVisible(true);
                            addFileButton4.setVisible(true);
                            addFileButton5.setVisible(true);
                            addFileButton6.setVisible(true);
                            addFileButton7.setVisible(true);
                            addFileButton8.setVisible(true);
                            addFileButton9.setVisible(true);
                            addFileButton10.setVisible(true);
                            addFileButton11.setVisible(true);
                            addFileButton12.setVisible(true);

                            filePathLabel1.setVisible(true);
                            filePathLabel2.setVisible(true);
                            filePathLabel3.setVisible(true);
                            filePathLabel4.setVisible(true);
                            filePathLabel5.setVisible(true);
                            filePathLabel6.setVisible(true);
                            filePathLabel7.setVisible(true);
                            filePathLabel8.setVisible(true);
                            filePathLabel9.setVisible(true);
                            filePathLabel10.setVisible(true);
                            filePathLabel11.setVisible(true);
                            filePathLabel12.setVisible(true);
                        } break;
                        case 1:{
                            file1OriginLabel.setVisible(true);
                            file2OriginLabel.setVisible(false);
                            file3OriginLabel.setVisible(false);
                            file4OriginLabel.setVisible(false);
                            file5OriginLabel.setVisible(false);
                            file6OriginLabel.setVisible(false);
                            file7OriginLabel.setVisible(false);
                            file8OriginLabel.setVisible(false);
                            file9OriginLabel.setVisible(false);
                            file10OriginLabel.setVisible(false);
                            file11OriginLabel.setVisible(false);
                            file12OriginLabel.setVisible(false);

                            file1OriginInputted.setVisible(true);
                            file2OriginInputted.setVisible(false);
                            file3OriginInputted.setVisible(false);
                            file4OriginInputted.setVisible(false);
                            file5OriginInputted.setVisible(false);
                            file6OriginInputted.setVisible(false);
                            file7OriginInputted.setVisible(false);
                            file8OriginInputted.setVisible(false);
                            file9OriginInputted.setVisible(false);
                            file10OriginInputted.setVisible(false);
                            file11OriginInputted.setVisible(false);
                            file12OriginInputted.setVisible(false);

                            addFileButton1.setVisible(true);
                            addFileButton2.setVisible(false);
                            addFileButton3.setVisible(false);
                            addFileButton4.setVisible(false);
                            addFileButton5.setVisible(false);
                            addFileButton6.setVisible(false);
                            addFileButton7.setVisible(false);
                            addFileButton8.setVisible(false);
                            addFileButton9.setVisible(false);
                            addFileButton10.setVisible(false);
                            addFileButton11.setVisible(false);
                            addFileButton12.setVisible(false);

                            filePathLabel1.setVisible(true);
                            filePathLabel2.setVisible(false);
                            filePathLabel3.setVisible(false);
                            filePathLabel4.setVisible(false);
                            filePathLabel5.setVisible(false);
                            filePathLabel6.setVisible(false);
                            filePathLabel7.setVisible(false);
                            filePathLabel8.setVisible(false);
                            filePathLabel9.setVisible(false);
                            filePathLabel10.setVisible(false);
                            filePathLabel11.setVisible(false);
                            filePathLabel12.setVisible(false);
                        }break;
                        case 2:{
                            file1OriginLabel.setVisible(true);
                            file2OriginLabel.setVisible(true);
                            file3OriginLabel.setVisible(false);
                            file4OriginLabel.setVisible(false);
                            file5OriginLabel.setVisible(false);
                            file6OriginLabel.setVisible(false);
                            file7OriginLabel.setVisible(false);
                            file8OriginLabel.setVisible(false);
                            file9OriginLabel.setVisible(false);
                            file10OriginLabel.setVisible(false);
                            file11OriginLabel.setVisible(false);
                            file12OriginLabel.setVisible(false);

                            file1OriginInputted.setVisible(true);
                            file2OriginInputted.setVisible(true);
                            file3OriginInputted.setVisible(false);
                            file4OriginInputted.setVisible(false);
                            file5OriginInputted.setVisible(false);
                            file6OriginInputted.setVisible(false);
                            file7OriginInputted.setVisible(false);
                            file8OriginInputted.setVisible(false);
                            file9OriginInputted.setVisible(false);
                            file10OriginInputted.setVisible(false);
                            file11OriginInputted.setVisible(false);
                            file12OriginInputted.setVisible(false);

                            addFileButton1.setVisible(true);
                            addFileButton2.setVisible(true);
                            addFileButton3.setVisible(false);
                            addFileButton4.setVisible(false);
                            addFileButton5.setVisible(false);
                            addFileButton6.setVisible(false);
                            addFileButton7.setVisible(false);
                            addFileButton8.setVisible(false);
                            addFileButton9.setVisible(false);
                            addFileButton10.setVisible(false);
                            addFileButton11.setVisible(false);
                            addFileButton12.setVisible(false);

                            filePathLabel1.setVisible(true);
                            filePathLabel2.setVisible(true);
                            filePathLabel3.setVisible(false);
                            filePathLabel4.setVisible(false);
                            filePathLabel5.setVisible(false);
                            filePathLabel6.setVisible(false);
                            filePathLabel7.setVisible(false);
                            filePathLabel8.setVisible(false);
                            filePathLabel9.setVisible(false);
                            filePathLabel10.setVisible(false);
                            filePathLabel11.setVisible(false);
                            filePathLabel12.setVisible(false);


                        } break;
                        case 3:{
                            file1OriginLabel.setVisible(true);
                            file2OriginLabel.setVisible(true);
                            file3OriginLabel.setVisible(true);
                            file4OriginLabel.setVisible(false);
                            file5OriginLabel.setVisible(false);
                            file6OriginLabel.setVisible(false);
                            file7OriginLabel.setVisible(false);
                            file8OriginLabel.setVisible(false);
                            file9OriginLabel.setVisible(false);
                            file10OriginLabel.setVisible(false);
                            file11OriginLabel.setVisible(false);
                            file12OriginLabel.setVisible(false);

                            file1OriginInputted.setVisible(true);
                            file2OriginInputted.setVisible(true);
                            file3OriginInputted.setVisible(true);
                            file4OriginInputted.setVisible(false);
                            file5OriginInputted.setVisible(false);
                            file6OriginInputted.setVisible(false);
                            file7OriginInputted.setVisible(false);
                            file8OriginInputted.setVisible(false);
                            file9OriginInputted.setVisible(false);
                            file10OriginInputted.setVisible(false);
                            file11OriginInputted.setVisible(false);
                            file12OriginInputted.setVisible(false);

                            addFileButton1.setVisible(true);
                            addFileButton2.setVisible(true);
                            addFileButton3.setVisible(true);
                            addFileButton4.setVisible(false);
                            addFileButton5.setVisible(false);
                            addFileButton6.setVisible(false);
                            addFileButton7.setVisible(false);
                            addFileButton8.setVisible(false);
                            addFileButton9.setVisible(false);
                            addFileButton10.setVisible(false);
                            addFileButton11.setVisible(false);
                            addFileButton12.setVisible(false);

                            filePathLabel1.setVisible(true);
                            filePathLabel2.setVisible(true);
                            filePathLabel3.setVisible(true);
                            filePathLabel4.setVisible(false);
                            filePathLabel5.setVisible(false);
                            filePathLabel6.setVisible(false);
                            filePathLabel7.setVisible(false);
                            filePathLabel8.setVisible(false);
                            filePathLabel9.setVisible(false);
                            filePathLabel10.setVisible(false);
                            filePathLabel11.setVisible(false);
                            filePathLabel12.setVisible(false);
                        }break;
                        case 4:{..
                        } break;
//this goes until case 12, each just continues to make one more item .setVisible = true until all 12 elements are visible.
                        default: break;
                    }
                } catch(NullPointerException e1){
                    e1.printStackTrace();
                    SwingUtilities.updateComponentTreeUI(landing);
                }


                SwingUtilities.updateComponentTreeUI(landing);


            }
        });

//additional code here that shouldn't be relevant to the issue
    } ```

我的问题来自实例化我的 GUI 的 Main class。我在创建 JFrame 之后设置外观,而这应该在创建任何 GUI 元素之前完成。这解决了我的问题