title: Why is this Swing Timer code is throwing a NullPointerException?
body: <p>I'm trying to animate an ellipse in Java using a Swing Timer.  I believe the following code should do the job, but when I run my program, the timer throws a <code>NullPointerException</code>.  Any idea as to why that may be?</p>
  
  <p>I've excluded the code with the mainline as it isn't relevant.  Here is my error, which is occurrs the first line in the <code>actionlistener</code>'s <code>actionperformed</code> method:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at Timer$MoveListener.actionPerformed(Timer.java:18)
  </code></pre>
  
  <p>Panel Class:</p>
  
  <pre><code>import java.awt.geom.*;
  
  public class Glitchpanel extends javax.swing.JPanel {
  
      private Ellipse ellipse;
      private java.awt.Dimension size;
      private Timer timer;
  
      public Glitchpanel() {
          super();
  
          ellipse = new Ellipse();
          size = new java.awt.Dimension(1000, 1000);
          timer = new Timer(ellipse, this);
          timer.start();
  
          this.setSize(size);
          this.setPreferredSize(size); 
          this.setBackground(java.awt.Color.WHITE);
  }
  
      public void paintComponent(java.awt.Graphics g) {
          super.paintComponent(g);
          java.awt.Graphics2D brush = (java.awt.Graphics2D) g;
          brush.draw(ellipse);
      }
  }
  </code></pre>
  
  <p>Timer Class:</p>
  
  <pre><code>import java.awt.geom.*;
  
  public class Timer extends javax.swing.Timer {
  
      private Glitchpanel glitch;
      private Ellipse ellipse;
  
      public Timer(Ellipse ellipse, Glitchpanel glitch) {
          super(100, null);
          ellipse = ellipse;
          glitch = glitch;
          this.addActionListener(new MoveListener());
      }
  
      private class MoveListener implements java.awt.event.ActionListener {
  
          public void actionPerformed(java.awt.event.ActionEvent e) {
              ellipse.setX(ellipse.getX()+1);
              ellipse.setY(ellipse.getY()+1);
              glitch.repaint();
          }
      }
  }
  </code></pre>
  
  <p>Shape Class (Trying to animate this object):</p>
  
  <pre><code>public class Ellipse extends java.awt.geom.Ellipse2D.Double {
  
      private Glitchpanel glitch;
      private double x, y, w, h;
  
      public Ellipse() {
          super();
  
          double x = 100;
          double y = 100;
          double w = 100;
          double h = 100;
          this.setFrame(x, y, w, h);
      }
  
      public void setX(double x2) {
          x = x2;
      }
  
      public void setY(double y2) {
          y = y2;
      }
  }
  </code></pre>