title: Array returning NullPointerException
body: <p>I've been creating a Snakes program that contains an array of <code>Segments</code>. In this case, I believe that it is vital I use an array as the order matters. The first problem is that even after I create a "new <code>Segment</code> instance", it doesn't display any square (which is the first segment of the snake). But then, when I put a <code>System.out.println()</code> statement inside of the paint method (shown later), it throws a <code>NullPointerException</code>. Here is the code:</p>
  
  <p>NOTE: the method <code>increaseSegmentCount()</code> was called once from an outside class. Also, this isn't the full code...</p>
  
  <p>Snake</p>
  
  <pre><code>import java.awt.Graphics;
  import java.awt.Point;
  
  public class Snake implements Entity {
      Point location;
      Segment[] segments;
      int segmentCount = 0;
  
      public Snake(Point location) {
          this.location = location;
          segments = new Segment[25];
      }
  
      public void increaseSegmentCount() {
  
          segments[segmentCount] = new Segment(new Point(location.x + (Segment.getSize().width * segments.length),
                  location.y + (Segment.getSize().height * segments.length)));
  
          segmentCount++;
  
      }
  
      public Segment[] getSegments() {
          return segments;
      }
  
      public int getSegmentCount() {
          return segmentCount;
      }
  
      @Override
      public void paint(Graphics g) {
          for (Segment segment : segments) {
              if (segment != null)
                  segment.paint(g);
          }
      }
  }
  </code></pre>
  
  <p><code>paint()</code> method after putting <code>System.out.println</code> statement in:</p>
  
  <pre><code>@Override
      public void paint(Graphics g) {
          for (Segment segment : segments) {
              if (segment != null)
                  System.out.println("Called")
                  segment.paint(g);
          }
      }
  </code></pre>
  
  <p><code>Segment</code> class (pretty straightforward class)</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Point;
  
  public class Segment {
      Point location;
  
      public Segment(Point location) {
          this.location = location;
      }
  
      public static Dimension getSize() {
          return new Dimension(20, 20);
      }
  
      public void paint(Graphics g) {
          g.setColor(Color.WHITE);
          g.fillRect(location.x, location.y, 20, 20);
      }
  }
  </code></pre>
  
  <p><code>paintComponent()</code> method in <code>View</code> class (for debugging purposes)</p>
  
  <pre><code>@Override
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (Entity entity : model.getEntities()) {
              entity.paint(g);
          }
      }
  </code></pre>
  
  <p>And finally the error (after inserting the print statement... there is none before I inserted it):</p>
  
  <pre><code>Called
  Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at Snake.paint(Snake.java:61)
      at View.paintComponent(View.java:50)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintToOffscreen(Unknown Source)
      at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
      at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
      at javax.swing.RepaintManager.paint(Unknown Source)
      at javax.swing.JComponent._paintImmediately(Unknown Source)
      at javax.swing.JComponent.paintImmediately(Unknown Source)
      at javax.swing.RepaintManager$4.run(Unknown Source)
      at javax.swing.RepaintManager$4.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.access$1200(Unknown Source)
      at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$500(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>
  
  <p>Any ideas? Also, please don't mark this as a duplicate and point me to another post that shows how to solve NPE's... I already know what to do and what's going on (for the most part), but this is a curious, thing I've come across and it's very confusing. Thanks.</p>