0
我一直在創建一個包含Segments
數組的Snakes程序。在這種情況下,我認爲這是至關重要的,因爲順序很重要。第一個問題是,即使在創建「新的Segment
實例」之後,它也不會顯示任何正方形(這是蛇的第一部分)。但是,當我在paint方法內部放置一個System.out.println()
語句(稍後顯示)時,它會拋出NullPointerException
。這裏是代碼:返回數組NullPointerExeption
注意:方法increaseSegmentCount()
被從外部類調用一次。
@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);
}
}
:此外,這是不完整的代碼...
蛇
把System.out.println
聲明後
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()
方法
paintComponent()
01方法類(調試)
@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的帖子...我已經知道該怎麼做,發生了什麼(大部分),但這是一件奇怪的事情我遇到過,這很混亂。謝謝。