Timer (Source not found)-Collection of common programming errors
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.(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();
}
}
-
You are not initializing your
TimerTask
variabletmtCheat
, hence theNullPointerException
.If you want to execute the method called
tmtCheat
on yourTimer
, you could use an anonymous class, or create one thatextends TimerTask
.timCheat.schedule(new TimerTask() { public void run() { // your code } }, 500);