Results 1 to 4 of 4
  1. #1
    RexBox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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:

    Java 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
    			}
    		}
    	}
    }
    And

    Java Code:
    public class Ball 
    
    {
    	public Ball()
    	
    	{}
    	public void update(MainRunner MR) //Every update
    	
    	{
    		System.out.println("works");
    	}
    }
    Now, it gives me the error;

    Exception in thread "Thread-3" java.lang.NullPointerException
    at MainRunner.run(MainRunner.java:27)
    at java.lang.Thread.run(Unknown Source)
    And it doesn't print anything at all? Any help would be really appriciated :)

    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 05:31 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Update doesn't work as expected?

    Are you sure you want to start() the Thread before the Ball variables are initialized?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    RexBox is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: Update doesn't work as expected?

    Quote Originally Posted by DarrylBurke View Post
    Are you sure you want to start() the Thread before the Ball variables are initialized?

    db
    Well, I tried it, and it works now! No idea why the other wouldn't work, but thanks alot :D.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Update doesn't work as expected?

    Quote Originally Posted by RexBox View Post
    No idea why the other wouldn't work
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Jar doesn't work
    By mad72584 in forum New To Java
    Replies: 35
    Last Post: 08-07-2011, 06:22 PM
  2. Replies: 0
    Last Post: 12-21-2010, 04:21 PM
  3. Replies: 4
    Last Post: 08-23-2010, 04:19 AM
  4. Replies: 1
    Last Post: 11-02-2009, 11:39 PM
  5. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 11:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •