我在启动Java应用程序时收到此错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at engine.Display.GamePanel.paintComponent(GamePanel.java:102)
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.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
应用程序没有关闭,它让我正常运行它没有视觉错误等等,但是当我启动应用程序时,我仍然会收到此错误消息。
我指向的唯一的代码是我的GamePanel类中的第102行,这是我的paintComponent方法:
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
//g.drawImage(test, picX, picY, picSizeX, picSizeY, null);
mao.Draw(picX, picY, g);
}
在mao.Draw中我们有这个:
public void Draw(int x, int y, Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(characterImage, x, y, null);
g2d.drawRect(x, y, getWidth(), getHeight());
}
我已经解决了这个问题,但我似乎无法找到错误。
答案 0 :(得分:2)
错误是在GamePanel.java的第102行引起的,我认为是
mao.Draw(picX, picY, g);
因为mao
尚未初始化,即在调用方法时具有值null
。可能您的应用程序尚未完全初始化,但GamePanel已经显示。
您可以执行以下操作来检查是否是这种情况:
if (mao != null) {
mao.Draw(picX, picY, g);
}
答案 1 :(得分:1)
NullPointerException
方法中的例外情况为paintComponent()
。
所以问题存在,而不是你的Draw()
方法。在paintComponent()
中,您访问mao
标识符,我认为该标识符是GamePanel
类的属性。
Swing中的所有绘画都是在EDT主题上完成的,我假设您初始化并设置此mao
属性不在EDT主题上=>这是一个同步问题。在EDT线程上分配mao
属性,或使其同步,以便正确访问它的多个线程看到它的真实值。
一旦或罕见的情况和您的应用程序正常运行,您会收到此异常,因为只有paintComponent()
将被中止,但后续尝试绘制组件可能会成功(如果mao
最终可见EDT线程或同时被修改。