{"id":1911,"date":"2022-08-30T15:20:28","date_gmt":"2022-08-30T15:20:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/timer-source-not-found-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:28","modified_gmt":"2022-08-30T15:20:28","slug":"timer-source-not-found-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/timer-source-not-found-collection-of-common-programming-errors\/","title":{"rendered":"Timer (Source not found)-Collection of common programming errors"},"content":{"rendered":"<p>I tried to create a timer and a timertask but I can&#8217;t get it to work \ud83d\ude41<\/p>\n<p>&#8220;Your post does not have much context to explain the code sections; please explain your scenario more clearly.&#8221;?<\/p>\n<p><b>Error<\/b><\/p>\n<p>Timer.sched(TimerTask, long, long) line: not available<br \/>\nSource not found.<\/p>\n<pre><code>Exception in thread \"main\" java.lang.NullPointerException\nat java.util.Timer.sched(Unknown Source)\nat java.util.Timer.schedule(Unknown Source)\nat Game.(Game.java:42)\nat Game.main(Game.java:25)\n<\/code><\/pre>\n<p><b>Game.java<\/b><\/p>\n<pre><code>import java.applet.*;\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.io.*;\nimport java.net.*;\nimport javax.swing.*;\nimport java.util.*;\nimport java.util.Timer;\nimport java.util.TimerTask;\nimport java.lang.Runnable;\n\npublic class Game implements ActionListener {\n\nprivate static Game hosmos2;\nprivate JFrame frmMain;\nprivate AntiCheat holyanticheat;\nprivate Dimension screen;\n\/\/private Engine holyengine;\nprivate int sx, sy;\nprivate Timer timCheat; \/\/ My Timer\nprivate TimerTask tmtCheat; \/\/ My TimerTask\n\npublic static void main(String[] args) throws FileNotFoundException, InterruptedException {\n\n    hosmos2 = new Game();\n}\n\nprivate Game() throws FileNotFoundException, InterruptedException {\n\n    frmMain = new JFrame(\"Hosmos 2\");\n    frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n    frmMain.setSize(1366, 768);\n    screen = Toolkit.getDefaultToolkit().getScreenSize();\n    sx = (int) ((screen.getWidth() - frmMain.getWidth()) \/ 2);\n    sy = (int) ((screen.getHeight() - frmMain.getHeight()) \/ 2);\n    frmMain.setLocation(sx, sy);\n    holyanticheat = new AntiCheat();\n    \/\/holyengine = new Engine();\n    timCheat = new Timer(); \/\/ Creates my timer\n    \/\/frmMain.add(holyengine);\n    frmMain.setVisible(true);\n    timCheat.schedule(tmtCheat, 500); \/\/ Schedule my TimerTask\n    Thread.sleep(1000);\n    timCheat.cancel();\n}\n\npublic void actionPerformed(ActionEvent e) {\n\n\n}\n\npublic void tmtCheat() throws IOException { \/\/ Void for my TimerTask\n\n    holyanticheat.Detect(); \/\/ Runs Detect() in AntiCheat.java\n}\n\n}\n<\/code><\/pre>\n<p><b>AntiCheat.java<\/b><\/p>\n<pre><code>import java.applet.*;\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.io.*;\nimport java.net.*;\nimport javax.swing.*;\nimport java.util.*;\n\npublic class AntiCheat {\n\nprivate BufferedReader highscore;\nprivate DataInputStream dis;\nprivate FileInputStream fis;\nprivate InputStreamReader isr;\nprivate String userscore, realscore;\n\npublic static void main(String[] args) {\n\n\n}\n\npublic AntiCheat() throws FileNotFoundException {\n\n    fis = new FileInputStream(\"data\/highscore.hs2\");\n    dis = new DataInputStream(fis);\n    isr = new InputStreamReader(dis);\n    highscore = new BufferedReader(isr);\n}\n\npublic void Detect() throws IOException { \/\/ Checks if you have cheated your score\n\n    userscore = highscore.readLine();\n    if (!userscore.equals(\"If you touch this then you won't be able to play noob ;)\")) {\n\n        for (int i = 0; i &lt; 1000; i++) {\n\n            realscore = Integer.toString(i);\n            for (int j = 0; j &lt; 173; j++) {\n\n                realscore = Integer.toString(realscore.hashCode());\n            }\n            if (userscore == realscore) {\n\n                break;\n            }\n            if (i == 999) {\n\n                JOptionPane.showMessageDialog(null, \"Du \u00e4r lika fattig som Malcolm lol.\");\n                System.exit(0);\n            }\n        }\n    }\n    highscore.close();\n}\n\n}\n<\/code><\/pre>\n<ol>\n<li>\n<p>You are not initializing your <code>TimerTask<\/code> variable <code>tmtCheat<\/code>, hence the <code>NullPointerException<\/code>.<\/p>\n<p>If you want to execute the method called <code>tmtCheat<\/code> on your <code>Timer<\/code>, you could use an anonymous class, or create one that <code>extends TimerTask<\/code>.<\/p>\n<pre><code>timCheat.schedule(new TimerTask() {\n  public void run() {\n    \/\/ your code\n  }\n}, 500);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:17:34. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I tried to create a timer and a timertask but I can&#8217;t get it to work \ud83d\ude41 &#8220;Your post does not have much context to explain the code sections; please explain your scenario more clearly.&#8221;? Error Timer.sched(TimerTask, long, long) line: not available Source not found. Exception in thread &#8220;main&#8221; java.lang.NullPointerException at java.util.Timer.sched(Unknown Source) at java.util.Timer.schedule(Unknown [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1911","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1911"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1911\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}