int th = 36;
int tw = 36;
for(int y = 0; y < th - 1; y++)
{
for(int x = 0; x < tw - 1; x++)
{
PVector p1 = normals[x][y];
//works
PVector p2 = normals[34][y];
// works
PVector p3 = normals[35][y];
// returns null object?
Your end of loop conditions are incorrect because they do not access the last element in the array. Try this
for(int y = 0; y < th; y++)
{
for(int x = 0; x < tw; x++)
{
Also in Java a 2D array is created as a ‘1D array’ of ‘1D arrays’ so your for-loops initialize an array
normals[y][x];
This is not causing a problem in your code because tb == th
a square 2D array if you want
normals[x][y];
then swap the for loops like this
{
for(int x = 0; x < tw; x++)
{
for(int y = 0; y < th; y++)
Hello @JALAJ_0106 ,
I suspect that you may have used the same loop to initialize array.
PVector p3 = normals[35][0];
// null There is no element 35!
println(p3);
Example:
int th = 36;
int tw = 36;
PVector normals[][] = new PVector[tw][th]; // Create a 2D array with `tw` columns and `th` rows
for(int y = 0; y < th - 1; y++) // <36-1 is 0 to 34
{
for(int x = 0; x < tw - 1; x++) // <36-1 is 0 to 34
{
normals[y][x] = new PVector(0, 0, 0); // Initialize each element with a new PVector
}
}
PVector p2 = normals[34][0];
println(p2);
// works
PVector p3 = normals[35][0];
// null There is no element 35!
println(p3);
Try and replace th-1 with th and see what happens.
print x am y in the loop to examine what you are doing.
:)