我正在制作一个游戏引擎而且一度陷入困境。
g.drawImage(BPaint.g2dToImage(bgo.render()), 100, 100, null);
执行NullPointerException,即使我检查连接到Image参数的所有内容。该代码用于paintComponent(Graphics)方法,如下所示。
@Override
public void paintComponent(Graphics g){
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
BWindow current = BWindow.getCurrentBWindow();
if (current != null) {
for (BGO bgo : current.getBGOs().getBgos()) {
g.drawImage(BPaint.g2dToImage(bgo.render()), 100, 100, null);
}
}
}
这是非常向前的,但是在阅读2种方法时你可能会陷入困境--bgo.render()和BPaint.g2dToImage。
BGO是游戏对象,具有Graphics2d方法
public Graphics2D render(){
if (bgsu == null) {
return sprite.getGraphics();
} else {
return bgsu.getRule();
}
}
忽略BGSU,因为它是null,并且没有使用,sprite是一个包含我想要获得的Graphics2D的对象。 现在转到BPaint.g2dToImage(Graphics2D),这是一种将Graphics2D转换为Image的方法。
public static Image g2dToImage(Graphics2D g){
JLabel j = new JLabel();
j.paintAll(g);
return j.createImage(null);
}
在检查了所有不应该为空的内容之后,我真的不知道原因。我希望你能告诉我。下面是NPE的完整堆栈跟踪。 (第34行是开头的方法行)
java.lang.NullPointerException
at sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation.java:732)
at sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.java:807)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1021)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3318)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3296)
at bdk.bjgl.components.BGraphics.paintComponent(BGraphics.java:34)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1572)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1495)
at javax.swing.RepaintManager.paint(RepaintManager.java:1265)
at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
at javax.swing.RepaintManager$4.run(RepaintManager.java:824)
at javax.swing.RepaintManager$4.run(RepaintManager.java:807)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731)
at javax.swing.RepaintManager.access$1300(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720)
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$1.doIntersectionPrivilege(ProtectionDomain.java:75)
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)