数组返回NullPointerExeption

时间:2016-03-06 10:37:42

标签: java arrays nullpointerexception

我一直在创建一个包含Segments数组的Snakes程序。在这种情况下,我认为使用数组至关重要,因为顺序很重要。第一个问题是,即使我创建了一个"新的Segment实例",它也不会显示任何方块(这是蛇的第一段)。但是,当我在paint方法中放置一个System.out.println()语句(稍后显示)时,它会抛出一个NullPointerException。这是代码:

注意:方法increaseSegmentCount()是从外部类调用一次的。此外,这不是完整的代码......

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()语句放入:

后的

System.out.println方法

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

Segment课程(相当简单的课程)

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()类中的

View方法(用于调试目的)

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

最后错误(在插入print语句之后......在我插入之前没有):

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)

有什么想法吗?另外,请不要将此标记为重复内容,并指出另一篇文章,说明如何解决NPE ...我已经知道该做什么以及发生了什么(最重要的)部分),但这是一个奇怪的,我遇到的事情,它非常令人困惑。感谢。

1 个答案:

答案 0 :(得分:2)

当你添加print语句时,你需要{} ...

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

您可能会考虑使用自动代码格式化程序,这会使缩进显而易见。