0

I've been creating a Snakes program that contains an array of Segments. In this case, I believe that it is vital I use an array as the order matters. The first problem is that even after I create a "new Segment instance", it doesn't display any square (which is the first segment of the snake). But then, when I put a System.out.println() statement inside of the paint method (shown later), it throws a NullPointerException. Here is the code:

NOTE: the method increaseSegmentCount() was called once from an outside class. Also, this isn't the full code...

Snake

import java.awt.Graphics;
import java.awt.Point;

public class Snake implements Entity {
    Point location;
    Segment[] segments;
    int segmentCount = 0;

    public Snake(Point location) {
        this.location = location;
        segments = new Segment[25];
    }

    public void increaseSegmentCount() {

        segments[segmentCount] = new Segment(new Point(location.x + (Segment.getSize().width * segments.length),
                location.y + (Segment.getSize().height * segments.length)));

        segmentCount++;

    }

    public Segment[] getSegments() {
        return segments;
    }

    public int getSegmentCount() {
        return segmentCount;
    }

    @Override
    public void paint(Graphics g) {
        for (Segment segment : segments) {
            if (segment != null)
                segment.paint(g);
        }
    }
}

paint() method after putting System.out.println statement in:

@Override
    public void paint(Graphics g) {
        for (Segment segment : segments) {
            if (segment != null)
                System.out.println("Called")
                segment.paint(g);
        }
    }

Segment class (pretty straightforward class)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

public class Segment {
    Point location;

    public Segment(Point location) {
        this.location = location;
    }

    public static Dimension getSize() {
        return new Dimension(20, 20);
    }

    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(location.x, location.y, 20, 20);
    }
}

paintComponent() method in View class (for debugging purposes)

@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Entity entity : model.getEntities()) {
            entity.paint(g);
        }
    }

And finally the error (after inserting the print statement... there is none before I inserted it):

Called
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Snake.paint(Snake.java:61)
    at View.paintComponent(View.java:50)
    at javax.swing.JComponent.paint(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._paintImmediately(Unknown Source)
    at javax.swing.JComponent.paintImmediately(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)

Any ideas? Also, please don't mark this as a duplicate and point me to another post that shows how to solve NPE's... I already know what to do and what's going on (for the most part), but this is a curious, thing I've come across and it's very confusing. Thanks.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Eames
  • 321
  • 2
  • 19

1 Answers1

2

you need { } for your if when you add the print statement...

if (segment != null) {
            System.out.println("Called")
            segment.paint(g);
}

You might consider using an automatic code formatter, which would make this obvious with the indentation.

Valette_Renoux
  • 366
  • 1
  • 9