Reason for "AWT-EventQueue-0" java.lang.NullPointerException error

35,356

Solution 1

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

In those lines, "path" does not refer to the attribute of your class but to a locale variable. Hence your attribute "path" is never initialized.

btw, 2 recommandations :

  • Never name a variable with the name of one of your attributes.
  • Your attributes should be private:

    private List path;

Solution 2

You are shadowing the variable path. A local variable with the same name is instantiated in the scope of the constructor PathPoints but the class member variable path is null in the method drawObjectPath. Replace

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

with

path = new ArrayList<Coordinates>();

Solution 3

In the PathPoints class constructor you are hiding the member variable path with a local variable:

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

This means that the member variable is never assigned a value and will be null when the drawObjectPath method is called.

To fix this, don't hide the member variable; use:

        int numberCoord;
        path = new ArrayList<Coordinates>();
        numberCoord = s.nextInt();
Share:
35,356
thehoule64
Author by

thehoule64

Updated on March 01, 2020

Comments

  • thehoule64
    thehoule64 about 4 years

    I'm creating a video game in Java and I am trying to retrieve a list of coordinate points and draw them onto the map, but every time I run my application I receive a "AWT-EventQueue-0" java.lang.NullPointerException and have not been able to figure out why it's pointing to null. Here is the error report I receive:

        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)
    

    The code of the error for the class PathPoints is here:

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

    The code for the class TestPanel is here:

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

    I have tested the constructor of PathPoints to see if it is correctly adding the values to the ArrayList and it is adding them correctly, so what part should I look at next to solve this?