我正在用 Java 创建一个视频游戏,我正在尝试检索坐标点列表并将它们绘制到地图上,但是每次运行我的应用程序时,我都会收到一个“AWT-EventQueue-0”java.lang.NullPointerException 和无法弄清楚为什么它指向空值。这是我收到的错误报告:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at game.PathPoints.drawObjectPath(PathPoints.java:23)
at game.TestPanel.paintComponent(TestPanel.java:64)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:866)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:581)
at javax.swing.JComponent.paintChildren(JComponent.java:866)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5145)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302)
at javax.swing.RepaintManager.paint(RepaintManager.java:1216)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1784)
at javax.swing.RepaintManager$3.run(RepaintManager.java:818)
at javax.swing.RepaintManager$3.run(RepaintManager.java:795)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:795)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:764)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:61)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:643)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
PathPoints 类的错误代码在这里:
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class PathPoints {
public PathPoints (Scanner s){
int numberCoord;
List<Coordinates> path = new ArrayList<Coordinates>();
numberCoord = s.nextInt();
for(int x = 0; x < numberCoord; x++){
Coordinates points = new Coordinates(s.nextInt(), s.nextInt());
path.add(points);
}
}
public void drawObjectPath(Graphics g){
g.setColor(Color.GREEN);
for(int n = 0; n < path.size()- 1; n++){
g.drawLine(((Coordinates)path.get(n)).x, ((Coordinates)path.get(n)).y, ((Coordinates)path.get(n+1)).x, ((Coordinates)path.get(n+1)).y);
g.drawLine(((Coordinates)path.get(n)).x, ((Coordinates)path.get(n)).y+1, ((Coordinates)path.get(n+1)).x, ((Coordinates)path.get(n+1)).y+1);
}
}
List path;
}
TestPanel 类的代码在这里:
public class TestPanel extends JPanel implements MouseListener
{
private static final long serialVersionUID = 1L; // Ignore this - It's just to get rid of a warning.
// Instance variable(s).
int x,y;
private Image backdrop;
PathPoints objectPath;
/**
* Constructor - loads a background image
*/
public TestPanel ()
{
try
{
ClassLoader myLoader = this.getClass().getClassLoader();
InputStream imageStream = myLoader.getResourceAsStream("resources/path_1.jpg");
backdrop = ImageIO.read(imageStream);
InputStream pointStream = myLoader.getResourceAsStream("resources/path_1.txt");
Scanner s = new Scanner (pointStream);
objectPath = new PathPoints(s);
}
catch (IOException e)
{
System.out.println ("Could not load: " + e);
}
addMouseListener(this);
}
/**
* This paint meethod draws the background image anchored
* in the upper-left corner of the panel.
*/
public void paintComponent (Graphics g)
{
g.drawImage(backdrop, 0, 0, null);
objectPath.drawObjectPath(g);
//Coordinates();
}
我已经测试了 PathPoints 的构造函数,看看它是否正确地将值添加到 ArrayList 并且正确添加它们,那么接下来我应该看哪一部分来解决这个问题?