1

EDITED (Updated code + question):

I'm working on creating a 2D top-down RPG and I'm having many troubles painting small 32x32 images (256 total). I've had success painting many squares of grading colors though. So i can confirm that the nested for-loop maps correctly. Based off of the code i have attached, can anyone help me understand where I go wrong?

public class TestClass extends JPanel {
public JPanel floor;
public Point[] imgPoints;
public Image[] tileImgs;
private Dimension d = new Dimension(512, 512);
private Graphics g;
private int size;


/*
 * This is the Floor class which is supposed to print level tiles 
 * tilesize is specified by 'int size'.
 * Constructor instantiates a JPanel which is
 * used inside a graphical interface (JFrame)
 */

public TestClass() throws IOException {
    floor = new JPanel();
    floor.setSize(new Dimension(512, 512));
    floor.setMinimumSize(new Dimension(512, 512));

    tileImgs = new Image[256];
    int k = 0;
    int xImg = 1;
    for (Image x : tileImgs) {
        if (xImg < 10){
            tileImgs[k] = ImageIO.read(this.getClass().getResource("/hyp/hyptosis" + "00" + xImg + ".png"));
        }
        else if (xImg < 100 && xImg >= 10){
            tileImgs[k] = ImageIO.read(this.getClass().getResource("/hyp/hyptosis" + "0" + xImg + ".png"));
            }
        else{
            tileImgs[k] = ImageIO.read(this.getClass().getResource("/hyp/hyptosis" + xImg + ".png"));   
        }
        k++;
    }
}

public void paint(Graphics g) {
    super.paint(g);
    setDoubleBuffered(true);
    int c = 0;
    int pointCount = 0;
    size = 32;
    for (int i = 0; i < d.getWidth(); i += size) {
        for (int j = 0; j < d.getHeight(); j += size) {
            pointCount++;
        }
    }
    c = 0;
    Point[] imgPoints = new Point[pointCount];
    for (int i = 0; i < d.getWidth(); i += size) {
        for (int j = 0; j < d.getHeight(); j += size) {
            imgPoints[c] = (new Point(i, j));
            c++;
        }
    }

    paintComponent();

}

// Paint level-tiles.
private void paintComponent() {
    int k = 0;
    for (Point q : imgPoints) {
        q = imgPoints[k];
        ImageObserver imgObs = null;
        int xx = (int) q.getX();
        int yy = (int) q.getY();
        g.drawImage(tileImgs[k], xx, yy, imgObs);
        this.imageUpdate(tileImgs[k], FRAMEBITS, xx, yy, size, size);
        repaint();
        k++;

    }
}

}

Error log from console:

> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at gameObjects.TestClass.paintComponent(TestClass.java:82)
    at gameObjects.TestClass.paint(TestClass.java:76)
    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.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.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$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$1200(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$500(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$JavaSecurityAccessImpl.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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at gameObjects.TestClass.paintComponent(TestClass.java:82)
    at gameObjects.TestClass.paint(TestClass.java:76)
    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.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.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$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$1200(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$500(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$JavaSecurityAccessImpl.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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) If that code snippet implies the class extends `JComponent`, then the `ImageObserver` can be specified as `this`. Every `JComponent` (and anything that inherits from it) implements `ImageObserver`. – Andrew Thompson Mar 06 '17 at 20:43
  • 1
    `this.getGraphics()` is the wrong approach, assuming you're using the default painting system – MadProgrammer Mar 06 '17 at 20:58
  • @MadProgrammer Thank you for showing interest in helping me solve the question. I reckon that I perhaps should have provided more detail. I'll update my question when I get home in 12hours (code is on my stationary), then I shall provide a minnimal example of the full code and perhaps more detail of the problem I encounter. – Tobias Andersen Mar 07 '17 at 09:29
  • *"I shall provide a minnimal example of the full code"* No, not the 'full code', I suggested an MCVE. `at gameObjects.TestClass.paintComponent(TestClass.java:82)` IF there was an MCVE, we could tell which line was line 82. As it is, I can only offer general advice. See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Mar 07 '17 at 23:53

0 Answers0