In processing (java library) I have created a function that is the synonym of an "add" for an arraylist but in a conventional array.
This function is very poor and only works for one type of class (only valid for PVector, not for int, or float...).
It should still work, but I get a NullPointerException on the third line (the one with the "posArray" variable).
I have been informed on the internet and I have seen that it is because posArray is an array with a certain length, but with null content. Still I don't understand why it doesn't work, since I am determining the content of it later (in the for loop).
Thanks in advance.
PVector[] pushVector(PVector[] poss, PVector pos) {
PVector[] temp = poss;
PVector[] posArray = new PVector[poss.length + 1];
for (int i = 0; i < temp.length; i++) {
posArray[i] = temp[i];
}
posArray[temp.length] = pos;
return posArray;
}
SOLVED
The problem is not in that code, the problem is that I use the code with an array that does not have length (
PVector ejemploDeArray;
).The solution would be to start the variable with a specific length:
PVector ejemploDeArray = new PVector[0];