Java.util.Timer错误

时间:2016-01-26 10:39:08

标签: java timer timertask

我是java的初学者,所以我希望问题的答案不是太明显。

我正在尝试使用Java.util.Timer和TimerTask类创建一个Timer-Tool。

我创建的TimerTask类正好位于TimeClass(我的主类)中,因此它们应该能够使用相同的变量(?)。

以下是代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import javax.sound.sampled.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 25.01.2016
  * @author 
  */

public class TimeClass extends Frame {
  // Anfang Attribute
  private JButton bt_timer1 = new JButton();
  private JNumberField n_timer1 = new JNumberField();
  private JTextField t_timer1 = new JTextField();
  private JNumberField n_delay1 = new JNumberField();
  private JButton bt_timer2 = new JButton();
  private JNumberField n_timer2 = new JNumberField();
  private JTextField t_timer2 = new JTextField();
  private JNumberField n_delay2 = new JNumberField();
  private JButton bt_timer3 = new JButton();
  private JNumberField n_timer3 = new JNumberField();
  private JTextField t_timer3 = new JTextField();
  private JNumberField n_delay3 = new JNumberField();
  private JButton bt_atimer1 = new JButton();
  private JTextField t_atimerrt1 = new JTextField();
  private JTextField t_atimerar1 = new JTextField();
  private JNumberField n_atimerdl1 = new JNumberField();
  private JTextField t_atimertw1 = new JTextField();
  private JButton bt_atimer2 = new JButton();
  private JTextField t_atimerrt2 = new JTextField();
  private JTextField t_atimerar2 = new JTextField();
  private JNumberField n_atimerdl2 = new JNumberField();
  private JTextField t_atimertw2 = new JTextField();
  private JButton bt_atimer3 = new JButton();
  private JTextField t_atimerrt3 = new JTextField();
  private JTextField t_atimerar3 = new JTextField();
  private JNumberField n_atimerdl3 = new JNumberField();
  private JTextField t_atimertw3 = new JTextField();
  private JButton bt_atimer4 = new JButton();
  private JTextField t_atimerrt4 = new JTextField();
  private JTextField t_atimerar4 = new JTextField();
  private JNumberField n_atimerdl4 = new JNumberField();
  private JTextField t_atimertw4 = new JTextField();
  public boolean timer1, timer2, timer3, atimer1, atimer2, atimer3, atimer4, firstStart;
  public int ti1, ti2, ti3, ati1, ati2, ati3, ati4;
  Timer t1, t2, t3, at1, at2, at3, at4, t0;
  private JLabel lb_timer1 = new JLabel();
  private JLabel lb_timer2 = new JLabel();
  private JLabel lb_timer3 = new JLabel();
  private JLabel lb_atimer1 = new JLabel();
  private JLabel lb_atimer2 = new JLabel();
  private JLabel lb_atimer3 = new JLabel();
  private JLabel lb_atimer4 = new JLabel();
  // Ende Attribute

  public TimeClass(String title) { 
    // TimeClass-Initialisierung

    super(title);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { dispose(); }
    });
    int frameWidth = 626; 
    int frameHeight = 407;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Panel cp = new Panel(null);
    add(cp);
    // Anfang Komponenten
timer1 = true; timer2 = true; timer3 = true; atimer1 = true; atimer2 = true; atimer3= true; atimer4 = true;
ti1 = 0; ti2 = 0; ti3 = 0; ati1 = 0; ati2 = 0; ati3 = 0; ati4 = 0;
bt_timer1.setBounds(16, 32, 57, 33);
bt_timer1.setText("Go!");
bt_timer1.setMargin(new Insets(2, 2, 2, 2));
bt_timer1.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent evt) { 
    bt_timer1_ActionPerformed(evt);
  }
});
cp.add(bt_timer1);
n_timer1.setBounds(80, 32, 33, 33);
n_timer1.setText("");
cp.add(n_timer1);
t_timer1.setBounds(120, 32, 121, 33);
cp.add(t_timer1);
n_delay1.setBounds(248, 32, 57, 33);
n_delay1.setText("");
cp.add(n_delay1);
bt_timer2.setBounds(16, 72, 57, 33);
bt_timer2.setText("Go!");
bt_timer2.setMargin(new Insets(2, 2, 2, 2));
bt_timer2.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent evt) { 
    bt_timer2_ActionPerformed(evt);
  }
});
cp.add(bt_timer2);
n_timer2.setBounds(80, 72, 33, 33);
n_timer2.setText("");
cp.add(n_timer2);
t_timer2.setBounds(120, 72, 121, 33);
cp.add(t_timer2);
n_delay2.setBounds(248, 72, 57, 33);
n_delay2.setText("");
cp.add(n_delay2);
bt_timer3.setBounds(16, 112, 57, 33);
bt_timer3.setText("Go!");
bt_timer3.setMargin(new Insets(2, 2, 2, 2));
bt_timer3.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent evt) { 
    bt_timer3_ActionPerformed(evt);
  }
});
cp.add(bt_timer3);
n_timer3.setBounds(80, 112, 33, 33);
n_timer3.setText("");
cp.add(n_timer3);
t_timer3.setBounds(120, 112, 121, 33);
cp.add(t_timer3);
n_delay3.setBounds(248, 112, 57, 33);
n_delay3.setText("");
cp.add(n_delay3);
bt_atimer1.setBounds(16, 168, 41, 33);
bt_atimer1.setText("Set");
bt_atimer1.setMargin(new Insets(2, 2, 2, 2));
bt_atimer1.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent evt) { 
    bt_atimer1_ActionPerformed(evt);
  }
});
cp.add(bt_atimer1);
t_atimerrt1.setBounds(64, 168, 89, 33);
cp.add(t_atimerrt1);
t_atimerar1.setBounds(160, 168, 97, 33);
cp.add(t_atimerar1);
n_atimerdl1.setBounds(264, 168, 41, 33);
n_atimerdl1.setText("");
cp.add(n_atimerdl1);
t_atimertw1.setBounds(312, 168, 89, 33);
cp.add(t_atimertw1);
bt_atimer2.setBounds(16, 208, 41, 33);
bt_atimer2.setText("Set");
bt_atimer2.setMargin(new Insets(2, 2, 2, 2));
bt_atimer2.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent evt) { 
    bt_atimer2_ActionPerformed(evt);
  }
});
cp.add(bt_atimer2);
t_atimerrt2.setBounds(64, 208, 89, 33);
cp.add(t_atimerrt2);
t_atimerar2.setBounds(160, 208, 97, 33);
cp.add(t_atimerar2);
n_atimerdl2.setBounds(264, 208, 41, 33);
n_atimerdl2.setText("");
cp.add(n_atimerdl2);
t_atimertw2.setBounds(312, 208, 89, 33);
cp.add(t_atimertw2);
bt_atimer3.setBounds(16, 248, 41, 33);
bt_atimer3.setText("Set");
bt_atimer3.setMargin(new Insets(2, 2, 2, 2));
bt_atimer3.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent evt) { 
    bt_atimer3_ActionPerformed(evt);
  }
});
cp.add(bt_atimer3);
t_atimerrt3.setBounds(64, 248, 89, 33);
cp.add(t_atimerrt3);
t_atimerar3.setBounds(160, 248, 97, 33);
cp.add(t_atimerar3);
n_atimerdl3.setBounds(264, 248, 41, 33);
n_atimerdl3.setText("");
cp.add(n_atimerdl3);
t_atimertw3.setBounds(312, 248, 89, 33);
cp.add(t_atimertw3);
bt_atimer4.setBounds(16, 288, 41, 33);
bt_atimer4.setText("Set");
bt_atimer4.setMargin(new Insets(2, 2, 2, 2));
bt_atimer4.addActionListener(new ActionListener() { 
  public void actionPerformed(ActionEvent evt) { 
    bt_atimer4_ActionPerformed(evt);
  }
});
cp.add(bt_atimer4);
t_atimerrt4.setBounds(64, 288, 89, 33);
cp.add(t_atimerrt4);
t_atimerar4.setBounds(160, 288, 97, 33);
cp.add(t_atimerar4);
n_atimerdl4.setBounds(264, 288, 41, 33);
n_atimerdl4.setText("");
cp.add(n_atimerdl4);
t_atimertw4.setBounds(312, 288, 89, 33);
cp.add(t_atimertw4);
setTitle("Grepolis Timer");


lb_timer1.setBounds(312, 40, 59, 19);
lb_timer1.setText("00:00:00");
cp.add(lb_timer1);
lb_timer2.setBounds(312, 80, 59, 19);
lb_timer2.setText("00:00:00");
cp.add(lb_timer2);
lb_timer3.setBounds(312, 120, 59, 19);
lb_timer3.setText("00:00:00");
cp.add(lb_timer3);
lb_atimer1.setBounds(408, 176, 59, 19);
lb_atimer1.setText("00:00:00");
cp.add(lb_atimer1);
lb_atimer2.setBounds(408, 216, 59, 19);
lb_atimer2.setText("00:00:00");
cp.add(lb_atimer2);
lb_atimer3.setBounds(408, 256, 59, 19);
lb_atimer3.setText("00:00:00");
cp.add(lb_atimer3);
lb_atimer4.setBounds(408, 296, 59, 19);
lb_atimer4.setText("00:00:00");
cp.add(lb_atimer4);
firstStart = true;
// Ende Komponenten

    setVisible(true);
   }

   // Anfang Methoden

  public static void main(String[] args) {
    new TimeClass("TimeClass");
  }
  public void bt_timer1_ActionPerformed(ActionEvent evt) {
    if (firstStart) {
      firstStart = false;
      t0.schedule(new CountTask(), 1000);
    }
    if (timer1) {
      t1 = new Timer();
      t1.schedule(new AlarmTask(),(n_timer1.getInt()*60000), (n_timer1.getInt()*60000)+(n_delay1.getInt()*1000));
      ti1 = n_timer1.getInt()*60;     
      bt_timer1.setText("Stop");
      timer1 = false;
    }
    else {
      t1.cancel();
      timer1 = true;
      bt_timer1.setText("Start");
    }

  }

  public void bt_timer2_ActionPerformed(ActionEvent evt) {
    if (timer2) {
      t2 = new Timer();
      t2.schedule(new AlarmTask(),(n_timer2.getInt()*60000),(n_timer2.getInt()*60000)+(n_delay2.getInt()*1000));      
      bt_timer2.setText("Stop");
      ti2 = n_timer2.getInt()*60; 
      timer2 = false;
    }
    else {
      t2.cancel();
      timer2 = true;
      bt_timer2.setText("Start");
    }
  }

  public void bt_timer3_ActionPerformed(ActionEvent evt) {
    if (timer3) {
      t3 = new Timer();
      t3.schedule(new AlarmTask(),(n_timer3.getInt()*60000),(n_timer3.getInt()*60000)+(n_delay3.getInt()*1000)); 
      ti3 = n_timer3.getInt()*60;      
      bt_timer3.setText("Stop");
      timer3 = false;
    }
    else {
      t3.cancel();
      timer3 = true;
      bt_timer3.setText("Start");
    }
  }

  public void bt_atimer1_ActionPerformed(ActionEvent evt) {
    // TODO hier Quelltext einfügen
  }

  public void bt_atimer2_ActionPerformed(ActionEvent evt) {
    // TODO hier Quelltext einfügen
  }

  public void bt_atimer3_ActionPerformed(ActionEvent evt) {
    // TODO hier Quelltext einfügen
  }

  public void bt_atimer4_ActionPerformed(ActionEvent evt) {
    // TODO hier Quelltext einfügen
  }

  public void update(){
    if (!timer1) {
     String s = "" + convertTime(ti1);
     lb_timer1.setText(s);
    }
    if (!timer2) {
      String s = convertTime(ti2);
      lb_timer2.setText(s);
    }
    if (!timer3) {
      String s = convertTime(ti3);
      lb_timer3.setText(s);
    }
    if (!atimer1) {
      String s = convertTime(ati1);
      lb_atimer1.setText(s);
    }
    if (!atimer2) {
      String s = convertTime(ati2);
      lb_atimer2.setText(s);
    }           
    if (!atimer3) {
      String s = convertTime(ati3);
      lb_atimer3.setText(s);
    }
    if (atimer4) {
      String s = convertTime(ati4);
      lb_atimer4.setText(s);
    }
  }

  public String convertTime(int t){
    int std = (t-(t%3600))/3600;
    int min = ((t%3600)-(t%60))/60;
    int sek = t%60;
    return ""+std+":"+min+":"+sek;
  }  


  class CountTask extends TimerTask
  {
    @Override public void run()
    {
      if (!timer1) {
        ti1--;
      }
      if (!timer2) {
        ti2--;
      }
      if (!timer3) {
        ti3--;
      }
      if (!atimer1) {
        ati1--;
      }
      if (!atimer2) {
        ati2--;
      }           
      if (!atimer3) {
        ati3--;
      }
      if (!atimer4) {
        ati4--;
      }
      update();
    }
  }        

  class AlarmTask extends TimerTask
  {
    @Override public void run()
    {
      try {
        File yourFile = new File("alarm.wav");
        AudioInputStream stream;
        AudioFormat format;
        DataLine.Info info;
        Clip clip;

        stream = AudioSystem.getAudioInputStream(yourFile);
        format = stream.getFormat();
        info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.open(stream);
        clip.start();
      }
      catch (Exception e) {

      }
    }
  }  
}

这里的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TimeClass.bt_timer1_ActionPerformed(TimeClass.java:245)
at TimeClass$2.actionPerformed(TimeClass.java:89)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.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)

我为不洁的编程感到抱歉 - 我是新手,我现在正在测试它。

感谢您的帮助,

卡罗尔

1 个答案:

答案 0 :(得分:0)

问题是永远不会创建Timer t0。 这导致NullPointerException在这里:

t0.schedule(new CountTask(), 1000);

致电t0 = new Timer();在使用之前。