title: How do I draw a Listener that extends MouseInputAdapter?
body: <p>I'm trying to use a class that extends <code>MouseInputAdapter</code> to draw freeform shapes, which I may later on make an animated object. </p>
  
  <p>I've seen a few answers, but they're using <code>addMouseMotionListener(this)</code>, which, as far as I understand you can't do with an object. My main problem is actually getting something to draw. I'm probably missing something fundamental, like where to initialize my <code>JPanel</code> or add my listener in the animate method. I get  <code>nullPointerException</code>s everytime i'm assuming it repaints. Either that or every time mouseDragged activates.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import javax.swing.event.MouseInputAdapter;
  import java.util.*;
  
  public class test {
  
      JFrame frame = new JFrame("Bouncing Vertices");
      MyDrawPanel drawPanel = new MyDrawPanel();
      private int delay = 5;
      public int z = 0;
      public int a = 0;
      public static int counter = 0;
      public static int[] xs;
      public static int[] ys;
      public static boolean isDone = false;
  
      public void animate() {
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(drawPanel);
          frame.setSize(800, 600);
          frame.setVisible(true);
          MyListener alpha = new MyListener();
          drawPanel.addMouseMotionListener(alpha);
          drawPanel.addMouseListener(alpha);
          while (true) {
              drawPanel.repaint();
              try {
                  Thread.sleep(delay);
              } catch (Exception ex) {
              }
          }
      }
  
      private class MyListener extends MouseInputAdapter {
  
          public void mouseDragged(MouseEvent arg0) {
              int x = arg0.getX();
              int y = arg0.getY();
  
              if (x != z || y != a) {
                  xs[counter] = x;
                  ys[counter] = y;
                  z = x;
                  a = y;
                  counter++;
              }
  
          }
  
          public void mouseReleased(MouseEvent arg0) {
              isDone = true;
          }
      }
  
      public static int[] getXs() {
          return xs;
      }
  
      public static int[] getYs() {
          return ys;
      }
  
      public static boolean getBoolean() {
          return isDone;
      }
  
      public static void setBoolean() {
          isDone = false;
      }
  
      public static void setArrays() {
          for (int i = 0; i &lt; ys.length; i++) {
  
              xs[counter] = 0;
              ys[counter] = 0;
          }
      }
  
      public static void main(String[] args) {
          test qwerty = new test();
          qwerty.animate();
      }
  }
  
  class MyDrawPanel extends JPanel {
  
      public void paintComponent(Graphics g) {
          g.setColor(Color.GRAY);
          g.fillRect(0, 0, this.getWidth(), this.getHeight());
          if (test.getBoolean() &amp;&amp; test.getXs().length &gt; 0) {
              g.setColor(Color.BLACK);
              g.drawPolyline(test.getXs(), test.getYs(), test.getYs().length);
              test.setBoolean();
              test.setArrays();
          }
      }
  }
  </code></pre>