Ah, that's the exception I meant. Not Null Pointer.
I think that there are several issues with your loop, mostly coming from how you're declaring/using various variables. This is some guess work, because a lot of understanding of code comes from understanding the intent, and I can only
just decipher what your variables mean. :3
Ahem!
Issue one is happening every iteration (Meaning that you're tossing out the previous array on every iteration, which might not be your intent).
mosagrid = new Mosaico[mosavis*2];
Issue two is that mosagrid is being indexed by mosanum.
Let's say that mosavis = 6.
Then, let's say that (mosaX2 - mosaX1) == 6, and (mosaY2 - mosaY1) ==6.
You have:
1 2 3 4 5 6 7
| mosanum = 0; for(int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) { mosagrid = new Mosaic[6*2]; mosanum++; } } |
If we follow the iterations:
i = 0, j = 0: mosanum = 0
i = 0, j = 1: mosanum = 1
i = 0, j = 2: mosanum = 2
...
i = 0, j = 5: mosanum = 5
i = 1, j = 0: mosanum = 6
i = 1, j = 1: mosanum = 7
...
i = 1, j = 5: mosanum = 11
i = 2, j = 0: mosanum = 12 (EXEPTION!)