-1

タイマーとタイマータスクを作成しようとしましたが、動作させることができません:(

「あなたの投稿には、コードセクションを説明するためのコンテキストがあまりありません。シナリオをより明確に説明してください。」?

エラー

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();
}

}
4

1 に答える 1

0

TimerTask変数を初期化していないtmtCheatため、NullPointerException.

tmtCheatで呼び出されたメソッドを実行する場合Timerは、匿名クラスを使用するか、extends TimerTask.

timCheat.schedule(new TimerTask() {
  public void run() {
    // your code
  }
}, 500);
于 2012-09-29T23:24:52.373 に答える