-5

I'm trying to make a basic timing loop, but I keep getting

java.lang.ArithmaticException / by zero error
    at Timer.advanceTime(Timer.java:24)
at Game.run(Game.java:79)
at java.lang.Thread.run(Thread.java:722)

here is my code:

public class Timer {
        private static final long NS_PER_SECOND = 1000000000L;
        private static final long MAX_NS_PER_UPDATE = 1000000000L;
        private static final int MAX_TICKS_PER_UPDATE = 100;
        private float ticksPerSecond;
        private long lastTime;
        public int ticks;
        public float a;
        public float timeScale = 1.0F;
        public float fps = 0.0F;
        public float passedTime = 0.0F;

        public Timer(float ticksPerSecond) {
                this.ticksPerSecond = ticksPerSecond;
                this.lastTime = System.nanoTime();
        }

        public void advanceTime() {
                long now = System.nanoTime();
                long passedNs = now - this.lastTime;
                this.lastTime = now;

                if (passedNs < 0L) passedNs = 0L;
                if (passedNs > 1000000000L) passedNs = 1000000000L;
                this.fps = (float)(1000000000L / passedNs);
                this.passedTime += (float)passedNs * this.timeScale * this.ticksPerSecond / 1.0E+009F;
                this.ticks = (int)this.passedTime;
                if (this.ticks > 100) this.ticks = 100;
                this.passedTime -= this.ticks;
                this.a = this.passedTime;
        }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 5
    Because you're dividing by zero. There's nothing else that can cause that error message. – Robert Harvey Nov 27 '13 at 03:22
  • -1 - you should include the relevant code in the question. See [help] –  Nov 27 '13 at 03:23
  • 1
    Do you know that the error is actually `ArithmeticException` and that `/ by zero error` means that the value by which you are dividing is `0`? Go to line 79 in your `Game.java` file and find out what's going on. – SimonT Nov 27 '13 at 03:23
  • @DavidWallace You can post it as an answer. But now you're gonna make me move his code into the question. – Robert Harvey Nov 27 '13 at 03:28
  • Line 79 may be `this.fps = (float)(1000000000L / passedNs);` – prime Nov 27 '13 at 03:50

2 Answers2

2

From the Javadoc for System.nanoTime

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis()

Therefore , the two consecutive calls that you make to System.nanoTime() are likely to have the same return value - which is why dividing by the difference between them gives you the exception.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

probably this will be the reason

            if (passedNs < 0L) passedNs = 0L; // if so passedNs becomes Zero
            if (passedNs > 1000000000L) passedNs = 1000000000L;
            this.fps = (float)(1000000000L / passedNs); // if passedNs is zero the error will occur 

You can't divide by value zero. It will throw an arithmetic exception. You have to avoid that by checking the 'passedNs' value is Zero or not or you have to handle that exception using a try catch block simply.

if(passedNs != 0L){
      this.fps = (float)(1000000000L / passedNs);
}

or

try{
    this.fps = (float)(1000000000L / passedNs);
}catch(Exception e){
    //handle the exception 
}
prime
  • 14,464
  • 14
  • 99
  • 131