zall
zall

Reputation: 585

Processing error: x cannot be resolved or is not a field (java with the processing app)

So I am using processing in java mode.

There is a function: ellipse(int,int,int,int) that draws an ellipse.

I want this program to add a bullet object to the bulletHolder arraylist when the mouse is clicked and then display ellipses for each of these bullet objects based on their x and y property values.

however an error is occurring saying that 'x cannot be resolved or is not a field'

on the line: ellipse((int) bulletHolder.get(i).x, (int) bulletHolder.get(i).y, 50, 50);

Here is my full code

public ArrayList<Bullet[]>  bulletHolder= new ArrayList<Bullet[]>();
int count = 0;
void setup(){
  size(500,500); 
}


void draw(){
  if(count!=0){
    for(int i =0; i<count; i++){
       ellipse(bulletHolder.get(i).x, bulletHolder.get(i).y, 50, 50); 
    }
  }
}

void mouseClicked(){
  bulletHolder.add(new Bullet(mouseX,mouseY));
  count++;
}

class Bullet {
  int x;
  int y;
  Bullet(int x,int y){
   this.x = x;
   this.y=y; 
  }
}

Upvotes: 1

Views: 9057

Answers (2)

 bulletHolder.add(new Bullet(mouseX,mouseY)); <--- here JVM trying to convert Bullet instance to array of Bullet instace. It can't be done

You are trying to use Bullet instance in to the ArrayList where it requires a Array of Bullet instance. That is why you rare getting that error.

You can either change your ArrayList to accept Bullet instance rather than an array or you can pass bullet array without changing the ArrayList declaration.

Upvotes: 3

Eran
Eran

Reputation: 393956

bulletHolder is an ArrayList that contains arrays of Bullet, so to get to x member of Bullet you have to add an array index :

bulletHolder.get(i)[index].x

Upvotes: 2

Related Questions