Results 1 to 4 of 4
Thread: Update doesn't work as expected?
- 10-06-2012, 04:25 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Update doesn't work as expected?
Hello, I am following a Java-Youtube tutorial, and while I I did some tesing, my code didn't work as expected.
It is ment to print "Works" 3 times, every 17 ticks.
Here is my code:
AndJava Code:import java.applet.Applet; public class MainRunner extends Applet implements Runnable { Ball b, b2, b3; public void start() //in the begin { Thread GameRunner = new Thread(this); //making the thread GameRunner.start(); //starting the thread b = new Ball(); b2 = new Ball(); b3 = new Ball(); } public void run() //all the way along { while (true) //making sure the game keeps running { b.update(this); b2.update(this); b3.update(this); try { Thread.sleep(17); //making the game wait for some time, to make sure it doesn't freeze } catch (InterruptedException e) { System.out.println("Failed!, because of" + e); //if it fails to wait for the exact amount of time, it prints the reason } } } }
Now, it gives me the error;Java Code:public class Ball { public Ball() {} public void update(MainRunner MR) //Every update { System.out.println("works"); } }
And it doesn't print anything at all? Any help would be really appriciated :)Exception in thread "Thread-3" java.lang.NullPointerException
at MainRunner.run(MainRunner.java:27)
at java.lang.Thread.run(Unknown Source)
NOTE: This is not my actualy program, I removed the unnecesary parts that didn't have to do anything with the error.Last edited by RexBox; 10-06-2012 at 04:31 PM.
- 10-06-2012, 04:58 PM #2
Re: Update doesn't work as expected?
Are you sure you want to start() the Thread before the Ball variables are initialized?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-06-2012, 05:04 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
- 10-06-2012, 05:54 PM #4
Re: Update doesn't work as expected?
Step through the lines of code in your mind. Or is it that you don't really know what Thread#start() does?
That's not a rhetorical question, by the way. If you don't understand the chronology -- the sequence -- of code execution, you're going to face bigger problems ahead.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Jar doesn't work
By mad72584 in forum New To JavaReplies: 35Last Post: 08-07-2011, 05:22 PM -
Java(TM) Web Start Launcher doesn't work after a software update for some users
By Richard Gillespie in forum Advanced JavaReplies: 0Last Post: 12-21-2010, 03:21 PM -
Simple SQL update doesn't work :( (Probebly easy error)
By Addez in forum New To JavaReplies: 4Last Post: 08-23-2010, 03:19 AM -
Why doesn't the panel update unless I drag the edges?
By Addez in forum New To JavaReplies: 1Last Post: 11-02-2009, 10:39 PM -
Why doesn't this work?
By Corder10 in forum New To JavaReplies: 1Last Post: 07-04-2009, 10:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks