I am trying to set the value in an array to a variable. Here is my code:
//init the array as a float
//I have tried to put a value in the brackets, but it returns a different error.
//I initialized it this way so I could call it from other methods
private float[] map;
// generate a "seed" for the array between 0 and 255
float x = generator.nextInt(256);
int n = 1;
// insert into the first 25 slots
while(n <= 25) {
// here's my problem with this next line
map[n] = x;
double y = generator.nextGaussian();
x = (float)Math.ceil(y);
n = n + 1;
}
I marked the line with my error, the error returned is: “uncaught exception thrown in…”. What am I doing wrong??? Thanks in advance.
EDIT—–
Here’s the whole exception:
Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
I’m using y to generate a random gaussian, then converting it into a float value and changing x into that float value
I’m pretty sure that It’s that line, because that’s what my compiler told me.
I’m guessing that you get one of two exceptions:
You are getting a
NullPointerExceptionbecause have initialized map tonull. Assign a non-null value using for example:You are getting an
IndexOutOfBoundsExceptionbecause you are using 1-based indexing instead of 0-based indexing.Change this:
To this
forloop: