title: how to solve Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
body: <p>I simply don't understand where all of those errors are coming from:</p>
  <pre><code>public class GameScreen extends JPanel {
  private int Width,Height,Gwidth,Gheight;//JFrame width and height and the game screen container widh and height
  private int Mx, My;// dragged mouse coordinates
  public static Player pl;
  public static Wave w;
  private Map map;
  private PathFinding dijkstra;
  private DepthFirstSearch dfs;
  private Path Enemypaths[];
  private boolean Redirect, BlockedPath, Pause, Hold, PathFound;
  private LinkedList&lt;Point&gt; Menu;// unit selection menu
  private int SelectedUnit;
  private int Clock;
  private DrawGameScreen DGS;
  public static int CubeSize = 40;
  
  
  public GameScreen(int Width, int Height) 
  {
      this.Width = Width;
      this.Height = Height;
      Gwidth = 1000;
      Gheight = 500;
      pl = new Player();
      w = new Wave();
      Enemypaths = new Path[50];
      Menu = new LinkedList&lt;Point&gt;();
      Pause = false;
      SelectedUnit = -1;
      Clock = 0;
      
      Menu.add(new Point(60,540));//in those points we put the unit selection menu
      Menu.add(new Point(120,540));
      Menu.add(new Point(180,540));
      
      for(int i = 0; i &lt; Enemypaths.length; i++)// initializing enemy paths
          Enemypaths[i] = new Path();
      
      map = new Map(pl.getUnits());
      dijkstra = new PathFinding(map);
      PathFound = true;
      dfs = new DepthFirstSearch(map);
      Redirect = true;
      BlockedPath = dfs.Traverse();
      
      
      DGS = new DrawGameScreen();
      this.setLayout(new BorderLayout());
      this.add(DGS,BorderLayout.CENTER);
      
      addMouseListener(new ClickListener());
      addMouseMotionListener(new ClickListener());
      Hold = false;
      gameStart();
      Clock();
  }
  public void StartNewGame() 
  {
      pl.getUnits().clear();
      pl.setLife(20);
      pl.setMoney(10);
      Wave.WaveId = 0;
      Clock = 0;
      for(int i = 0; i &lt; 15; i++) 
      {
          for(int j = 0; j &lt; 25; j++) 
          {
              map.UpdateTile(j, i, 0);
              dfs.UpdateTile(j, i, 0);
              dijkstra.UpdateTile(j, i, 0);
          }
      }
  }
  
  public void gameStart() 
  {
      Thread gameThread = new Thread() 
      {
          public void run() 
          {
              while(true)// to make sure our game thread wont stop.
              {
                  if(!Pause) 
                  {
                      if(pl.getLife() &gt; 0 )// not a game over 
                      {
                          if(Clock == 1) w.NewWave(5, 25);// starting our enemy waves with 5 enemies at the start
                          if(PathFound) 
                          {
                              repaint();
                              if(w.AddEnemy(pl))// checking if we have an active wave of enemies before adding more.
                              {// for each new enemy summoned by the thread:
                                  Enemy e = w.getEnemeis().get(w.getEnemyCount() - 1);
                                  Enemypaths[w.getEnemyCount() - 1].setP(new LinkedList&lt;Point&gt;(dijkstra.FindShortestPath((int)e.getEx()/CubeSize, (int)e.getEy()/CubeSize)));// find the shortest path for that enemy
                                  e.setMoves(Enemypaths[w.getEnemyCount() - 1].getP());//set this path as his path.
                              }
                              
                              for(int i = 0; i &lt;w.getEnemyCount(); i++)//making each enemy move 
                              {
                                  Enemy e = w.getEnemeis().get(i);
                                  if(e != null) e.Move();
                              }
                              
                              if(Redirect)// redirecting the enemy path if found a shorter way
                              {
                                  PathFinding();
                                  Redirect = false;
                              }
                              
                              for(int i = 0; i &lt; pl.getUnits().size(); i++)// making each unit shoot an enemy 
                              {
                                  Unit u = pl.getUnits().get(i);
                                  if(u != null) u.Shoot(w.getEnemeis(), pl);
                              }
                          }
                          
                      }
                  }
              }
          }
      };
      gameThread.start();
  }
  
  public void PathFinding() 
  {
      Thread dijkstraThread = new Thread() 
      {
          public void run() 
          {
              PathFound = false;
              for(Enemy e : w.getEnemeis()) 
              {
                  if(e != null) 
                  {
                      if(e.getType() == 1 || e.getType() == 3) 
                      {
                          //Enemy e = w.getEnemeis().get(w.getEnemyCount() - 1);
                          Enemypaths[w.getEnemyCount() - 1].setP(new LinkedList&lt;Point&gt;(dijkstra.FindShortestPath((int)e.getEx()/CubeSize, (int)e.getEy()/CubeSize)));// find the shortest path for that enemy
                          e.setMoves(Enemypaths[w.getEnemyCount() - 1].getP());
                      }
                  }
              }
              PathFound = true;
          }
      };
      dijkstraThread.start();
  }
  
  
  public void Clock() 
  {
      Thread T = new Thread() 
      {
          public void run() 
          {
              while (true)
              {
                  if (!Pause &amp;&amp; pl.getLife() &gt; 0)
                      Clock++;
                  try
                  {
                      Thread.sleep(1000);
                  }
                  catch (InterruptedException ex) {}  
              }
          }
      };
      T.start();  
  }
  
  class DrawGameScreen extends JPanel
  {
      Unit u;
      Enemy e;
      
      @Override
      public void paintComponent(Graphics g) 
      {
          super.paintComponent(g);
          g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/BackGroundMap.png&quot;),0,0,Gwidth,Gheight,this);// drawing the background
          g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/Enemy Base.png&quot;),0,7*CubeSize,this);//drawing the player and enemy bases
          g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/Enemy Base.png&quot;),24*CubeSize,7*CubeSize,this);
          //drawing game components
          DrawGrid(g);
          DrawUnits(pl,g);
          DrawEnemies(w,g);
          DrawUnitPlacement(Mx,My,g);
          DrawIcons(g);
          //drawing prices
          g.setColor(Color.BLACK);
          g.setFont(new Font(&quot;Arial&quot;,Font.BOLD,16));
          g.drawString(&quot;5&quot;,60,600);
          g.drawString(&quot;20&quot;,120,600);
          g.drawString(&quot;50&quot;,180,600);
          //drawing player info
          g.drawString(&quot;money: &quot; + pl.getMoney(),300,545);
          g.drawString(&quot;life: &quot; + pl.getLife(),300,565);
          //drawing game info
          g.setFont(new Font(&quot;Arial&quot;,Font.BOLD,50));
          g.drawString(&quot;Wave: &quot; + Wave.getWaveId(),450,580);
          g.drawString(&quot;Timer: &quot; + (Clock/10) + &quot;.&quot; + (Clock%10),475,600);
          if(Clock &lt; 10) 
          {
              g.setFont(new Font(&quot;Arial&quot;,Font.BOLD,150));
              g.drawString(&quot;New Game!&quot;,200,250);
          }
          if(pl.getLife() &lt;= 0)//Game Over accrued
          {
              g.setColor(Color.RED);
              g.setFont(new Font(&quot;Arial&quot;,Font.BOLD,105));
              g.drawString(&quot;Game Over!&quot;,200,250);
              g.setFont(new Font(&quot;Arial&quot;,Font.BOLD,80));
              g.drawString(&quot;Score: &quot;+ pl.getMoney(),200,150);
          }
          
      
      }
      
      public void DrawGrid(Graphics g) 
      {
          g.setColor(Color.GRAY);
          for(int i = CubeSize; i &lt;= Gheight; i+= CubeSize)
              g.drawLine(0,i,Gwidth,i);// drawing the lines of the grid from left to right
          
          for(int i = CubeSize; i &lt;= Gwidth; i+= CubeSize)
              g.drawLine(i,0,i,Gheight);// drawing the lines of the grid from down to MaxHeight.
      }
      
      public void DrawUnits(Player pl,Graphics g) 
      {
          for(int i = 0; i &lt; pl.getUnits().size(); i++)// drawing the units on the screen in the correct position
          {
               u = pl.getUnits().get(i);
               u.drawBullet(g, this);
               g.drawImage(u.getImage(),u.getX()*CubeSize,u.getY()*CubeSize,CubeSize,CubeSize,this);
          }
      }
      
      public void DrawEnemies(Wave w,Graphics g) //drawing the enemies on screen with health bars.
      {
          for(int i = 0; i &lt; w.getEnemyCount(); i++)
          {
              e = w.getEnemeis().get(i);
              if(e != null) 
              {
                  int healthbar = (int)e.getHp();
                  int losthealth = (int)e.getMaxHp();
                  g.drawImage(e.getImage(),(int)e.getEx(),(int)e.getEy(),CubeSize,CubeSize,this);
                  
                  g.setColor(Color.RED);
                  g.fillRect((int)e.getEx(),(int)e.getEy()-5,losthealth*CubeSize,5);
                  g.setColor(Color.GREEN);
                  g.fillRect((int)e.getEx(),(int)e.getEy()-5,healthbar*CubeSize,5);
              }
          }
      }
      
      public void DrawUnitPlacement(int Mx, int My, Graphics g) //Checking if we can place a unit in this location
      {
          if(Mx &lt; Gwidth &amp;&amp; My &lt; Gheight)
          {
              if(Mx/CubeSize == 0 &amp;&amp; My/CubeSize == 7 || Mx/CubeSize == 24 &amp;&amp; My/CubeSize == 7)
                      BlockedPath = true;//wont allow to place units in the start or the finish of the map.
              else 
              {
                  dfs.ResetVisits();
                  if(dfs.Nodes[My/CubeSize][Mx/CubeSize].getType() == 0)//if the mouse is over a clear tile
                  {
                      dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 1);//Set the tile that we want to place our unit in as blocked
                      BlockedPath = dfs.Traverse();//traverse through the graph with our wanted tile as blocked to check if there is a way to bypass it
                      dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 0);// return the tile to its original state to make sure it wont intervene with the enemies path finding.
                  }
                  else BlockedPath = true;//if the mouse is over a blocked tile(type == 1)
                  
              }                                   
              
              for(int i = 0; i &lt; Menu.size(); i++) //setting the mouse courser to Green or Red depending on the tile for placement.   
              {                                    // also drawing the dragging animation for the unit.
                  if(SelectedUnit != 0)// checking if a unit was chosen
                  {
                      if(My &lt; Gheight &amp;&amp; Mx &lt; Gwidth)// in bounds of the game screen
                      {
                          if(map.GetType(Mx/CubeSize, My/CubeSize) == 0 &amp;&amp; !BlockedPath) g.setColor(Color.GREEN);
                          else g.setColor(Color.RED);
                          g.fillRect((Mx/CubeSize)*CubeSize,My*CubeSize,CubeSize,CubeSize);// painting the block 
                      }
                      if(SelectedUnit == 1) g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/Artillery.png&quot;),Mx*CubeSize,My*CubeSize,CubeSize,CubeSize,this);
                      if(SelectedUnit == 2) g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/Rockets.png&quot;),Mx*CubeSize,My*CubeSize,CubeSize,CubeSize,this);
                      if(SelectedUnit == 3) g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/AoeGun.png&quot;),Mx*CubeSize,My*CubeSize,CubeSize,CubeSize,this);
                  }
              }
          }   
      }
      
      public void DrawIcons(Graphics g)// drawing paused, resume and new game icons
      {
          if(Pause) {
              g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/resume.png&quot;),700,540,this);
          }
          else {
              g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/resume.png&quot;),700,540,this);
          }
          g.drawImage(Toolkit.getDefaultToolkit().getImage(&quot;GameAssets/NewGame.png&quot;),800,540,this);
      }
      
      @Override
      public Dimension getPreferredSize() 
      {
          return (new Dimension(Width,Height));
      }
      
  }
  
  class ClickListener extends MouseAdapter
  {
      public void MousePressed(MouseEvent e) 
      {
          if (e.getX() &gt;= 800 &amp;&amp; e.getX() &lt;= 960 &amp;&amp; e.getY() &gt;= 550 &amp;&amp; e.getY() &lt;= 610)
              
          if(pl.getLife() &gt; 0)// don't do these actions unless the game is still going on
          {
              if (e.getX() &gt;= 700 &amp;&amp; e.getX() &lt;= 765 &amp;&amp; e.getY() &gt;= 540 &amp;&amp; e.getY() &lt;= 620)// setting click pause
              {
                  if (Pause) Pause = false;
                  else Pause = true;
              }
              
              for(int i = 0; i &lt; Menu.size(); i++)// selecting a unit from the unit menu
              {
                  if(e.getX() &gt;= Menu.get(i).getX() &amp;&amp; e.getX() &lt;= Menu.get(i).getX()+CubeSize &amp;&amp; e.getY() &gt;= Menu.get(i).getY() &amp;&amp; e.getY() &lt;= Menu.get(i).getY()+CubeSize) 
                  {
                      SelectedUnit = i;
                      Hold = true;
                      updateLocation(e);
                      break;
                  }
                  else Hold = false;
              }
              
          }
      }
      
      public void mouseDragged(MouseEvent e)// dragging the unit into the game screen
      {
          if(Hold) updateLocation(e);
      }
      
      public void mouseReleased(MouseEvent e)// releasing units into the game screen
      {
          if(SelectedUnit != -1 &amp;&amp; pl.getLife() &gt; 0) 
          {
              if(My &lt;= Gheight &amp;&amp; Mx &lt;= Gwidth) 
              {
                  if(map.GetType(Mx/CubeSize, My/CubeSize) == 0 &amp;&amp; !BlockedPath) 
                  {
                      if(SelectedUnit == 0 &amp;&amp; pl.getMoney() &gt;= 5)// if an artillery is selected 
                      {
                          pl.setMoney(pl.getLife() - 5);
                          pl.getUnits().add(new Artillery(Mx/CubeSize,My/CubeSize));
                          Redirect = true;
                          map.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
                          dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
                          dijkstra.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
                      }
                      if(SelectedUnit == 1 &amp;&amp; pl.getMoney() &gt;= 20)// if an artillery is selected 
                      {
                          pl.setMoney(pl.getLife() - 20);
                          pl.getUnits().add(new Rockets(Mx/CubeSize,My/CubeSize));
                          Redirect = true;
                          map.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
                          dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
                          dijkstra.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
                      }
                  }
              }
          }
          Hold = false;
          Mx = 0;
          My = 0;
      }
      
      public void updateLocation(MouseEvent e)// save dragged mouse coordinates
      {
          Mx = e.getX();
          My = e.getY();  
      }
  }
  </code></pre>
  <p>}</p>
  <pre><code>Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
      at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
      at java.base/java.util.Objects.checkIndex(Objects.java:359)
      at java.base/java.util.ArrayList.get(ArrayList.java:427)
      at gameRelated.GameScreen$DrawGameScreen.DrawEnemies(GameScreen.java:271)
      at gameRelated.GameScreen$DrawGameScreen.paintComponent(GameScreen.java:214)
      at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
      at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
      at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
      at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
      at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
      at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
      at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
      at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5271)
      at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
      at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
      at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
      at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
      at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060)
      at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
      at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:75)
      at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:112)
      at java.desktop/java.awt.Container.paint(Container.java:2003)
      at java.desktop/java.awt.Window.paint(Window.java:3949)
      at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:876)
      at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
      at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
      at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
      at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
      at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
      at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
      at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
      at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
      at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
  </code></pre>
  <p>all of those errors repeat them self like 3 more times in a row for some reason.</p>