-1

i am getting illegalArgumentException on error line.

      private final static int SPOT_DIAMETER=100;
      private int viewWidth;

      .

      .

      .


     @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
      viewHeight=h;
      viewWidth=w;

     }

"viewWidth-SPOT_DIAMETER" returns integer,isnt it?How i solve this error?

    public void addNewSpot(){



   int x=random.nextInt(viewWidth-SPOT_DIAMETER);//error line
   .

   .
   .
    }
Moustafa51
  • 23
  • 4

3 Answers3

2

As specified by Random.nextInt(int) JavaDoc, you'll get that exception if you call it with a negative number, which is probably what's happening.

Parameters:
n - the bound on the random number to be returned. Must be positive.

Returns:
the next pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive) from this random number generator's sequence

Throws:
IllegalArgumentException - if n is not positive
JSlain
  • 566
  • 3
  • 20
0

Are you sure you're putting a number, which bigger, than 100 (your SPOT_DIAMETER), when you're calling the onSizeChanged() method? Check it, because as JSlain already said, this exception throws when randomizer is taking a negative number.

Also, maybe your method doesn't work/called and your viewWidth variable is empty, when you're trying to call a randomizer.

  • i learned that the variable viewWidth returns always 0 so random.nextInt(0) gives exception and i'm searching that why getWidth() returns 0 now ? – Moustafa51 Sep 02 '14 at 23:40
  • I don't know. How can i understand why `getWidth()` returns 0 if i didn't see your code?) And you have `random.nextInt(-100)`, not just 0 if `viewWidth` contains 0. Try to debug. – linuxedhorse Sep 02 '14 at 23:49
-1

Use viewWidth.SPOT_DIAMETER assuming it is a public constant.

Jonathon Anderson
  • 1,162
  • 1
  • 8
  • 24