0

I'm making a game engine and got stuck at one point.

g.drawImage(BPaint.g2dToImage(bgo.render()), 100, 100, null);

does NullPointerException, even though I check EVERYTHING that connects to Image parameter. The code is used in paintComponent(Graphics) method, written below.

@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);
        }
    }
}

This is pretty forward, but you may be stuck while reading at 2 methods - bgo.render() and BPaint.g2dToImage.

BGO is game object, which has Graphics2d method

public Graphics2D render(){
    if (bgsu == null) {
        return sprite.getGraphics();
    } else {
        return bgsu.getRule();
    }
}

Ignoring BGSU, because it is null, and not used, sprite is an object containing Graphics2D that I want to get. Now to BPaint.g2dToImage(Graphics2D), a method turning Graphics2D to Image.

public static Image g2dToImage(Graphics2D g){
    JLabel j = new JLabel();
    j.paintAll(g);
    return j.createImage(null);
}

After checking everything that should not be null, I don't really know the cause. I hope you can tell me. Below is full stacktrace of NPE. (line 34 is line of method in beginning)

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)
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • @ChiefTwoPencils as said before stacktrace, "line 34 is line of method in beginning", which is g.drawImage(BPaint.g2dToImage(bgo.render()), 100, 100, null); – Kirill Semyonkin Feb 26 '16 at 20:20
  • Ah, you must mean the beginning of your post not the code the line actually references.... – ChiefTwoPencils Feb 26 '16 at 20:24
  • I am not able to reproduce this. Maybe, you can break it down and post a [sscce](http://sscce.org/). – Stefan Dollase Feb 26 '16 at 20:52
  • From the [JavaDocs, `Component#createImage`](https://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#createImage-int-int-) *"The return value may be null if the component is not displayable."* and since the label isn't added to anything,'it's not displayable. Instead create a BufferedImage the size of the labels preferred size, use its Graphics context to print the label (you should use printAll over paintAll) – MadProgrammer Feb 26 '16 at 21:29
  • Why do your BGO objects return `java.awt.Graphics2D` instead of `java.awt.Image`? – Thomas Kläger Feb 26 '16 at 21:45

0 Answers0