title: Referencing issue with ActionListener class
body: <p>I have problem referencing the Timer to the ActionListener class. I want to stop the timer after Java displays the dialog box that displays the time and starts again after clicking on "Yes". </p>
  
  <p>This is what I currently have:</p>
  
  <pre><code>public class AlarmClock 
  {
      public static void main(String[] args)
      {
          boolean status = true;
  
          Timer t = null;
  
          ActionListener listener = new TimePrinter(t);
          t = new Timer(10000, listener);
  
          t.start();
  
          while(status)
          {
          } 
      }
  }
  
  class TimePrinter implements ActionListener
  {   
      Timer t;
  
      public TimePrinter(Timer t)
      {
          this.t = t;
      }
      public void actionPerformed(ActionEvent event)
      {   
          t.stop();                //To stop the timer after it displays the time
  
          Date now = Calendar.getInstance().getTime();
          DateFormat time = new SimpleDateFormat("HH:mm:ss.");
  
          Toolkit.getDefaultToolkit().beep();
          int choice = JOptionPane.showConfirmDialog(null, "The time now is "+time.format(now)+"\nSnooze?", "Alarm Clock", JOptionPane.YES_NO_OPTION);
  
          if(choice == JOptionPane.NO_OPTION)
          {
              System.exit(0);
          }
          else
          {
              JOptionPane.showMessageDialog(null, "Snooze activated.");
              t.start();           //To start the timer again
          }
      }
  }
  </code></pre>
  
  <p>However, this code gives a null pointer exception error. Is there any other way I can reference the Timer?</p>