计时器(未找到来源)

时间:2012-09-29 23:09:44

标签: java timer timertask

我试图创建一个计时器和一个时间任务,但我无法让它工作:(

“您的帖子没有太多的背景来解释代码部分;请更清楚地解释您的情况。”

错误

Timer.sched(TimerTask,long,long)行:不可用
找不到来源。

Exception in thread "main" java.lang.NullPointerException
at java.util.Timer.sched(Unknown Source)
at java.util.Timer.schedule(Unknown Source)
at Game.<init>(Game.java:42)
at Game.main(Game.java:25)

Game.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
import java.lang.Runnable;

public class Game implements ActionListener {

private static Game hosmos2;
private JFrame frmMain;
private AntiCheat holyanticheat;
private Dimension screen;
//private Engine holyengine;
private int sx, sy;
private Timer timCheat; // My Timer
private TimerTask tmtCheat; // My TimerTask

public static void main(String[] args) throws FileNotFoundException, InterruptedException {

    hosmos2 = new Game();
}

private Game() throws FileNotFoundException, InterruptedException {

    frmMain = new JFrame("Hosmos 2");
    frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmMain.setSize(1366, 768);
    screen = Toolkit.getDefaultToolkit().getScreenSize();
    sx = (int) ((screen.getWidth() - frmMain.getWidth()) / 2);
    sy = (int) ((screen.getHeight() - frmMain.getHeight()) / 2);
    frmMain.setLocation(sx, sy);
    holyanticheat = new AntiCheat();
    //holyengine = new Engine();
    timCheat = new Timer(); // Creates my timer
    //frmMain.add(holyengine);
    frmMain.setVisible(true);
    timCheat.schedule(tmtCheat, 500); // Schedule my TimerTask
    Thread.sleep(1000);
    timCheat.cancel();
}

public void actionPerformed(ActionEvent e) {


}

public void tmtCheat() throws IOException { // Void for my TimerTask

    holyanticheat.Detect(); // Runs Detect() in AntiCheat.java
}

}

AntiCheat.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;

public class AntiCheat {

private BufferedReader highscore;
private DataInputStream dis;
private FileInputStream fis;
private InputStreamReader isr;
private String userscore, realscore;

public static void main(String[] args) {


}

public AntiCheat() throws FileNotFoundException {

    fis = new FileInputStream("data/highscore.hs2");
    dis = new DataInputStream(fis);
    isr = new InputStreamReader(dis);
    highscore = new BufferedReader(isr);
}

public void Detect() throws IOException { // Checks if you have cheated your score

    userscore = highscore.readLine();
    if (!userscore.equals("If you touch this then you won't be able to play noob ;)")) {

        for (int i = 0; i < 1000; i++) {

            realscore = Integer.toString(i);
            for (int j = 0; j < 173; j++) {

                realscore = Integer.toString(realscore.hashCode());
            }
            if (userscore == realscore) {

                break;
            }
            if (i == 999) {

                JOptionPane.showMessageDialog(null, "Du är lika fattig som Malcolm lol.");
                System.exit(0);
            }
        }
    }
    highscore.close();
}

}

1 个答案:

答案 0 :(得分:0)

您没有初始化TimerTask变量tmtCheat,因此NullPointerException

如果要在tmtCheat上执行名为Timer的方法,可以使用匿名类,或创建一个extends TimerTask

timCheat.schedule(new TimerTask() {
  public void run() {
    // your code
  }
}, 500);