nullPointerExeption

I’m working on creating bullets for a remake of diep.io, but am new to arrays. I received an error that said nullPointerExeption, and looked it up to find out what I did wrong, but found that it means my array hasn’t been defined. I’ve double checked that I’m running the code, and I define the array’s at the beginning of my code.

// at the beginning
float[] arrayBulletAngle; 
float[] arrayBulletTotalDistance; 
float[] arrayBulletHealth; 
float[] arrayBulletSpeed; 
int numberOfPlayerBullets = 1; 

// I call this in the draw loop
void createBullet(float angle){ 
  numberOfPlayerBullets ++; 
  arrayBulletAngle[numberOfPlayerBullets] = angle; 
  arrayBulletTotalDistance[numberOfPlayerBullets] = 0; 
  arrayBulletHealth[numberOfPlayerBullets] = bulletHealth; 
  arrayBulletSpeed[numberOfPlayerBullets] = angle; 
}
2 Likes

If my memory is good, you must initialize your array or define a size for this array before use it

for example,

float[] arrayOne = new float[10]; // for a 10 float elements array
int[] arrayTwo = {0,1,2,3,4,5,6,7,8,9}; // for a 10 int elements pre declared array

without a size declaration, you’v got a NullPointerExeption.
The size of an array is fixed. If you want an expandable array, List seems much better

2 Likes

Thank you so much! That explains my problems.

1 Like
1 Like