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

I create a video game in Java and I try to get a list of coordinate points and draw them on the map, but every time I run my application, I get "AWT-EventQueue-0" java.lang.NullPointerException and could not figure out why is it pointing to null. Here is the error report I get:

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 error code for the PathPoints class 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 TestPanel class is here:

  public class TestPanel extends JPanel implements MouseListener { private static final long serialVersionUID = 1L; // Ignore this - It 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 checked the PathPoints constructor to see if it is adding values ​​to the ArrayList correctly and it is adding them correctly, so what part should I look further to solve this problem?

+4
source share
4 answers
 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 these lines, the "path" does not refer to the attribute of your class, but to the locale variable. Therefore, your path attribute is never initialized.

btw, 2 recommendations:

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

    closed path to the list,

+5
source

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

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

with

 path = new ArrayList<Coordinates>(); 
+1
source

In the constructor of the PathPoints class PathPoints you hide the path member variable with a local variable:

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

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

To fix this, do not hide the member variable; Application:

  int numberCoord; path = new ArrayList<Coordinates>(); numberCoord = s.nextInt(); 
+1
source
 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; } 
0
source

All Articles