我启动了一个GUI应用程序,下面的例外多次出现(大约8个)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Colors cannot be null
at java.awt.GradientPaint.<init>(GradientPaint.java:71)
at it.soltec.ibox.client.components.IBFrameTitlePane.paintTitleBackground(IBFrameTitlePane.java:1065)
at it.soltec.ibox.client.components.IBFrameTitlePane.paintComponent(IBFrameTitlePane.java:1008)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JViewport.paint(JViewport.java:747)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at it.soltec.ibox.client.IBoxMainFrame$MyPanel.paint(IBoxMainFrame.java:6595)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at java.awt.Window.paint(Window.java:3375)
at it.soltec.ibox.client.IBoxMainFrame.paint(IBoxMainFrame.java:6014)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
这是异常引用的代码
protected void paintTitleBackground(Graphics g) {
if ( g instanceof Graphics2D) {
Graphics2D g2 = (Graphics2D) g;
Paint savePaint = g2.getPaint();
boolean isSelected = frame.isSelected();
int w = getWidth();
// selectedTitleColor = Color.WHITE;
// selectedTitleGradientColor = Color.ORANGE;
if (isSelected) {
GradientPaint titleGradient =
new GradientPaint(0, 0, selectedTitleColor,
(int) (w * .75), 0, selectedTitleGradientColor);
导致异常的确切行是: GradientPaint titleGradient = GradientPaint(0,0,selectedTitleColor,
注意变量selectedTitleColor已经在类的其他地方设置了一个值:: public void installDefaults(){ selectedTitleColor = UIManager.getColor(“InternalFrame.activeTitleBackground”);
答案 0 :(得分:1)
编译器不撒谎。尝试在调试模式下运行代码并设置断点,以便查看selectedTitleColor
和selectedTitleGradientColor
的值。我保证其中至少有一个是null
。