Java でビデオ ゲームを作成していて、座標点のリストを取得してマップに描画しようとしていますが、アプリケーションを実行するたびに "AWT-EventQueue-0" java.lang.NullPointerException を受け取り、 nullを指している理由を理解できませんでした。私が受け取ったエラーレポートは次のとおりです。
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 に正しく追加されているかどうか、およびそれらが正しく追加されているかどうかを確認しました。これを解決するには、次にどの部分を見ればよいでしょうか?