JMenu中的例外情况

时间:2011-03-12 03:44:52

标签: java swing jmenu

我用Java开发了一个应用程序..

出现的问题是,首次点击后,如果用户再次点击菜单,则会发生以下异常。

由于显示源未知,因此我无法对其进行调试...

任何人都可以帮我解决这个问题吗?


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at sun.font.FontDesignMetrics$MetricsKey.init(Unknown Source)
        at sun.font.FontDesignMetrics.getMetrics(Unknown Source)
        at sun.swing.SwingUtilities2.getFontMetrics(Unknown Source)
        at javax.swing.JComponent.getFontMetrics(Unknown Source)
        at sun.swing.SwingUtilities2.getFontMetrics(Unknown Source)
        at javax.swing.plaf.synth.SynthMenuItemUI.paint(Unknown Source)
        at javax.swing.plaf.synth.SynthMenuUI.paint(Unknown Source)
        at javax.swing.plaf.synth.SynthMenuUI.update(Unknown Source)
        at javax.swing.JComponent.paintComponent(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JLayeredPane.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
        at javax.swing.RepaintManager.paint(Unknown Source)
        at javax.swing.JComponent._paintImmediately(Unknown Source)
        at javax.swing.JComponent.paintImmediately(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

我假设第一次点击是显示菜单和菜单项的那一次。如果用户然后点击一个菜单项,并且你得到一个例外,我敢打赌你还没有完全实例化与菜单相关的一些菜单项组件。

希望这有帮助。

John Doner

答案 1 :(得分:0)

 at sun.font.FontDesignMetrics$MetricsKey.init(Unknown Source)

检查字体的实例化。您需要在某处使用new Font(name, ...)