-1

I tried to create a timer and a timertask but I can't get it to work :(

"Your post does not have much context to explain the code sections; please explain your scenario more clearly."?

Error

Timer.sched(TimerTask, long, long) line: not available
Source not found.

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

}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Daniel Hammer
  • 39
  • 1
  • 5

1 Answers1

0

You are not initializing your TimerTask variable tmtCheat, hence the NullPointerException.

If you want to execute the method called tmtCheat on your Timer, you could use an anonymous class, or create one that extends TimerTask.

timCheat.schedule(new TimerTask() {
  public void run() {
    // your code
  }
}, 500);
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • Do you mean that i have not written this? "tmtCheat = new TimerTask();" Cus when i do i get this error for "TimerTask()" "Cannot instantiate the type TimerTask" – Daniel Hammer Sep 29 '12 at 23:26
  • @DanielHammer: That's because `TimerTask` is an abstract class. Either use an anonymous class, like in my example, or create a class that extends `TimerTask`. – João Silva Sep 29 '12 at 23:28
  • I have used the extends example before and it worked. But then it went to the void run(). And i want to be able to use multiple timers/timertasks to do multiple stuff. But i was not able to because every timer/timertask would do all the stuff in the run() void :( – Daniel Hammer Sep 29 '12 at 23:31
  • I use "e.getSource() == btnMusic" when i check what button was pressed in my "actionPerformed" void. Can i do the same in the "run" void to check what timer called the timertask? – Daniel Hammer Sep 29 '12 at 23:33
  • Why do you need multiple timers? I only see one in your example code. – João Silva Sep 29 '12 at 23:35
  • I'm porting my game from C# and it has multiple timers and they all do different stuff. Anyway, your example worked and it solves my problem. Thank you :) – Daniel Hammer Sep 29 '12 at 23:41
  • @DanielHammer: Alright, glad I could help. Good luck with the rest of your game. – João Silva Sep 29 '12 at 23:42