Trouble expanding and writing to array of objects

Hello. Slightly embarrassing, I appear to be having trouble with this class array.

Getting NullPointerException, but I’ve checked by dumping the length of the array to the console, and it’s definitely getting expanded.I’ve tried a few different arrangements with the same result, and don’t understand why I can’t seem to write to the array… I’ve done it many times. Although working with classes is new to me, but I seem to be able to populate my other arrays just fine, so I’m kinda not sure at all what’s wrong.

Reduced code, currently set up in a loop, but I can’t seem to get it to work even if I hard-code a single element. (Not included, but I do have a class ant() that is properly set up as well, and cellXY is also properly set). Hopefully someone can tell me what silly thing I’m not seeing:

int antsPerNest = 10;
int antAmt = 0;
ant[] a;


void setup() {
  a = new ant[0];
}


void setType() {
  a = (ant[]) expand(a, antAmt + antsPerNest);
  for (int i = antAmt; i < a.length; i++) {
    a[i] = new ant(cellXY.x, cellXY.y);
  }
  antAmt+= antsPerNest;
}

(post deleted by author)

Hello. Thank-you. cellXY is already being set correctly. I just did not include that in my minimal paste.

cell[] c;
int cellAmt = cellRows * cellCols;


void setup() {
  c = new cell[cellAmt];
  int i = 0;
  for (int y = 0; y < cellRows; y++) {
    for (int x = 0; x < cellCols; x++) {
      c[i] = new cell((x * cellSize) + (cellSize /2), (y * cellSize) + (cellSize /2));
      i++;
    }
  }
}


class cell {
    PVector cellXY;

cell(float x, float y) {
    cellXY = new PVector(x, y);
  }

}

And this does give the expected output. I know cellXY is being set.

Also, the cellXY variable in the OP is being used within the cell class where it’s set, so there should be no problem with casting. (setType() is a function within the cell class)

your code should be runnable

also, the two codes do not match because in the 2nd code ant doesn’t appear

int antsPerNest = 10;
int antAmt = 0;
ant[] a;
CellXY cellXY=new CellXY(54, 166);

void setup() {
  size(660, 660);
  a = new ant[0];
  setType();
}

void draw() {
  for (ant a1 : a) {
    a1.display();
  }
}

void setType() {
  a = (ant[]) expand(a, antAmt + antsPerNest);
  for (int i = antAmt; i < a.length; i++) {
    cellXY=new CellXY(random(width), random(height));
    a[i] = new ant(cellXY.x, cellXY.y);
  }
  antAmt+= antsPerNest;
}

class CellXY {
  float x, y;

  //constr
  CellXY(float x, float y) {
    this.x = x;
    this.y = y;
  }
}
class ant {

  float x, y;

  //constr
  ant(float x, float y) {
    this.x = x;
    this.y = y;
  }

  void display() {
    //
    circle (x, y, 4);
  }
}

1 Like

The second code block is not related. It is only there to show the first reply that I have, indeed, set the cellXY variable - As I stated in my OP.

No, my code is not runnable because it’s a reduced code snippet containing the relevant information only. My program does a lot more than shown, and I’m not going to re-write it to try to integrate your methods. That should be un-necessary, and neither does it answer my question.

1 Like

I see your problem with posting code.

The idea is to make a MCVE which should be runnable to demonstrate the problem:

Why use ArrayList<> instead of array with append()?

3 Likes