4

我正在用 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 并且正确添加它们,那么接下来我应该看哪一部分来解决这个问题?

4

4 回答 4

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

在这些行中,“路径”不是指您的类的属性,而是指语言环境变量。因此,您的属性“路径”永远不会被初始化。

顺便说一句,2 条建议:

  • 永远不要用你的一个属性的名字来命名一个变量。
  • 您的属性应该是私有的:

    私有列表路径;

于 2013-03-29T15:58:15.980 回答
1

您正在隐藏变量path。同名的局部变量在构造函数的范围内被实例化,PathPoints但类成员变量pathnull方法中drawObjectPath。代替

List<Coordinates> path = new ArrayList<Coordinates>();

path = new ArrayList<Coordinates>();
于 2013-03-29T15:57:44.843 回答
1

在类构造函数中,您将使用局部变量PathPoints隐藏成员变量:path

        int numberCoord;
        List<Coordinates> path = new ArrayList<Coordinates>();
        numberCoord = s.nextInt();

这意味着成员变量永远不会被赋值,并且会nulldrawObjectPath方法被调用时赋值。

要解决此问题,请不要隐藏成员变量;采用:

        int numberCoord;
        path = new ArrayList<Coordinates>();
        numberCoord = s.nextInt();
于 2013-03-29T15:57:57.960 回答
0
public class StationDatabase1 {

    File file = new File("lrt.txt");


public ArrayList<Station> getAll() throws SQLException, ClassNotFoundException, FileNotFoundException {
        ArrayList<Station> station = new ArrayList<>();
        Scanner input = new Scanner(file);
        try {
            while (input.hasNextLine()) {
                String id = input.nextLine();
                String name = input.nextLine();
                Station temp = new Station(id, name);
                station.add(temp);

              }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
        Set newSet = new LinkedHashSet(station);
        station.clear();
        station.addAll(newSet);
        return station;
    }
于 2014-03-01T08:48:44.133 回答