gpt4 book ai didi

java - 数组返回 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 03:51:42 25 4
gpt4 key购买 nike

我一直在创建一个 Snakes 程序,其中包含 Segments 数组。在这种情况下,我认为使用数组至关重要,因为顺序很重要。第一个问题是,即使在我创建“新 Segment 实例”之后,它也不会显示任何正方形(这是蛇的第一段)。但是,当我输入 System.out.println() 时在 Paint 方法中声明(稍后显示),它会抛出 NullPointerException 。这是代码:

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

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

最后是错误(插入打印语句后...在插入之前没有错误):

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 的帖子...我已经知道该做什么以及发生了什么(大部分),但这是一件奇怪的事情我遇到过,非常困惑。谢谢。

最佳答案

当您添加打印语句时,您的 if 需要 { }...

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

您可以考虑使用自动代码格式化程序,这会使缩进变得明显。

关于java - 数组返回 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com