We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The script below represents a matrix of random PVectors.
I want to lock some indexes from that matrix. (4 of them, one at each corner):
I tried to concat() 2 different arrayLists for such end (the random values matrix + another one with the fixed values) but didn´t work. I also tried to append() the values independently but got same result.
Now I´m trying to use the set() method, so I could eventually replace the required item in the specified position in the list, but a NullPointerException warning is thrown (line 143). I wonder if the use of the arrayList() method is the correct here. thx
int elements=10;
PVector centro;
ArrayList <Agent> a = new ArrayList <Agent>();
Repeler rep;
//ArrayList <FixedPoint> fp = new ArrayList <FixedPoint>();
Agent ag;
/** //list of PVectors to lock in the arrayList
loc1= new PVector(100, 100);
loc2= new PVector(100, 700);
loc3= new PVector(1300, 100);
loc4= new PVector(1300, 700);
*/
PVector loc1= new PVector(100, 100);
Agent vec1= new Agent (loc1);// 2nd constructor
void setup() {//______________________________________________________________
size(1400, 800);
frameRate(30);
smooth();
centro= new PVector(width/2, height/2);
rep = new Repeler(centro);
//a.add(vec1);
for (int i=0; i<elements; i++) {
a.add(new Agent());
println(a.size());
}
a.set(0, vec1);
}
void draw() {//_______________________________________________________________
background(99);
for (int i=0; i<a.size(); i++) {
ag= a.get(i);
PVector repulse = rep.attract(ag);
ag.update(repulse);
ag.run();
}
rep.display();
}
class Agent {//_______________________________________________________________
PVector loc, vel, acc;
PVector lol;
Agent() {//1st constructor
loc= new PVector(random(width), random(height));
vel= new PVector (0, 0);
acc= new PVector (random (-.01, .01), random (-.01, .01));
}
Agent (PVector _lol) {//2nd constructor for set() mode
lol=_lol;
}
void run() {
move();
display();
limits();
drawLine();
}
void update(PVector force) {
vel.add(force);
}
void drawLine() {
for (int i=0; i<a.size(); i++ ) {
Agent other= a.get(i);
stroke(255, 0, 0);
line(loc.x, loc.y, other.loc.x, other.loc.y);
}
}
void move() {
vel.add(acc);
loc.add(vel);
acc.mult(0);
}
void limits() {
if (loc.x > width-10 || loc.x < 10 ) {
vel.x = -vel.x ;
} else if (loc.y > height-10 || loc.y < 10) {
vel.y = -vel.y;
}
}
void display() {
stroke(222);
noFill();
ellipse(loc.x, loc.y, 5, 5);
}
}
/**
class FixedPoint extends Agent {//____________________________________________
PVector loc1, loc2, loc3, loc4;
FixedPoint() {
loc1= new PVector(100, 100);
loc2= new PVector(100, 700);
loc3= new PVector(1300, 100);
loc4= new PVector(1300, 700);
}
void run() {
display();
}
void display() {
fill(0, 0, 255);
ellipse(loc1.x, loc1.y, 5, 5);
ellipse(loc2.x, loc2.y, 5, 5);
ellipse(loc3.x, loc3.y, 5, 5);
ellipse(loc4.x, loc4.y, 5, 5);
}
}
*/
class Repeler {//_____________________________________________________________
PVector rep;
Repeler(PVector _rep) {
rep= _rep;
}
PVector attract(Agent ag) {
PVector force= PVector.sub(rep, ag.loc);// here is where NPE warning is thrown
float dist=force.mag();
dist=constrain(dist, 1., 2.);
force.normalize();
//force.mult(-1);
return force;
}
void display() {
noStroke();
fill(65, 111);
ellipse(rep.x, rep.y, 50, 50);
}
}
Answers
Hello hope am not late, idk what your problem is but i know why the NPE occurs: on this line:
PVector force= PVector.sub(rep, ag.loc);
the loc property of ag is null, this is because at line 34:
a.set(0, vec1);
u put vec1 in first index, but
Agent vec1= new Agent (loc1);// 2nd constructor
and
You dont initialize the loc PVector in vec1, so just use the other constructor for vec1:
Agent vec1= new Agent ();// 1st constructor
@giorgos123. U r right, actually I needed to create a subclass to cast the updated PVectors to make the system work. Anyway, once this was done, the problem was solved, thx!