RuntimeException error - How/Why am I getting it?

Can anyone explain to me what a RuntimeException is?

I’m getting it from these bits of code when I try to do something when the secs get to 0 :


 t = new Thread(){
			 public void run() {
				  while(game_active) {
				  secs = secs - 1;
				   try 
				   {
					Thread.sleep(1000);
				   } catch (InterruptedException e) {
					e.printStackTrace();
				   }
				  }
				 }
				};
				t.start();


public void update(){
if( secs == 0)
		  {
			  GameOver();
		  }
}
// GameOver
	  private void GameOver()
	  {
		 Dialog.alert("Game Over!");
	  }
	  

Or if i change it i get an illegalstateexcpetion, however if i use System.exit(0) it simply closes it, no errors.

Any ideas?

Can you post the stack trace (all of the error) please?

Secs is being used / changed in separate threads. Unless you know what you’re doing, you really should NOT be making additional threads. Why do you want to count seconds like this? Don’t you have a deltaTime somewhere in your gameloop, which you could use to count time passed?

EDIT: Forgot an important “NOT” there…phew

I’m using the BlackBerry JDE so i dont have deltaTime so i need to count in this way, I tried using a Timer and TimerTask but that was given me errors as well.

It tells me there is no stack trace.
It just repeats saying, NO STACK TRACE

how i can i get the same result using the two threads?

Two threads wont cause exceptions like this, he’s pointing out that multiple threads handling the same non-volatile or synchronized variable will have problems. One could update the value the same time as the other, and you could have funky effects.

Why are you using multiple threads?

You could use this timer instead of a thread:


public class SimpleTimer {
	private long cachedTime;

	public SimpleTimer() {
		reset();
	}

	public void reset() {
		cachedTime = System.currentTimeMillis();
	}

	public long getElapsed() {
		return System.currentTimeMillis() - cachedTime;
	}
}

Example to keep track of seconds:


/* Before you inititialize your first thread */
final SimpleTimer secondCounter = new SimpleTimer();

/* In your run method: */
if (secondTimer.getElapsed() >= 1000) {
	seconds -= 1;
	/* Timer has reached one second, reset the timer */
	secondTimer.reset();
}

There’s no need to use a thread for a task so simple, use a simple timer! ;D
lol, hope it helps mate.

A very good solution :slight_smile:

Yeah, that solution to keep track of time is sufficient. That crazy thread stuff you’re doing is a bad idea mmmkay?

Thanks, i didnt know it would be that easy :slight_smile:

Simple: Your program runs timely into an exceptionally problematic section of code.

Sorry, couldn’t resist ;D

Well done :stuck_out_tongue: