NullPointerException Error
in
Programming Questions
•
1 year ago
Hey guys,
I seem to have run into a problem with my code involving 'NullPointerException' which I'm not too sure what it means. When I try to run my code this error message pops up:
Exception in thread "Animation Thread" java.lang.NullPointerException
at leztrydisagain_w_oPVECTOR$Fish.detectBound(leztrydisagain_w_oPVECTOR.java:293)
at leztrydisagain_w_oPVECTOR.draw(leztrydisagain_w_oPVECTOR.java:66)
at processing.core.PApplet.handleDraw(PApplet.java:1631)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:613)
My code reads:
class Fish {
PVector pos, vel;
PVector waveSpeed;
float wave, amp;
float xScale, bSize;
float tankX, tankY, tankW, tankH;
//boolean alive;
color bColor;
Fish(PVector pos, PVector vel, float wave, float amp, float xScale, float bSize) {
pos= pos;
vel= vel;
wave= wave;
amp= amp;
xScale= xScale;
bSize= bSize;
bColor= color(random(255), random(255), random(255));
}
void detectBound() {
if (pos.x+ bSize/2 > width) { //this is the part it highlights
pos.y= width- bSize/2;
vel.x*= -1;
}
if (pos.x- bSize/2 < 0) {
pos.x= bSize;
vel.x*= -1;
}
if (pos.y+ bSize/2 > height) {
pos.y= height- bSize/2;
vel.y*= -1;
}
if (pos.y- bSize/2 < 0) {
pos.y= bSize/2;
vel.y*= -1;
}
}
void update() { //changing the position of fish at random speed
pos.x += vel.x;
pos.y += vel.y+2*sin(wave)*amp;
}
boolean detectCollision(Fish otherFish) {
if (abs(pos.x- otherFish.pos.x)< (bSize/2+ otherFish.bSize/2) && abs(pos.y- otherFish.pos.y)< (bSize/2+ otherFish.bSize/2)) {
return true;
}
return false;
}
void bounce(Fish otherFish) {
float angle = atan2(pos.y - otherFish.pos.y, pos.x - otherFish.pos.x);
vel.x = 1 * cos(angle);
vel.y = 1 * sin(angle);
otherFish.vel.x = 1 * cos(angle - PI);
otherFish.vel.y = 1 * sin(angle - PI);
}
void destroy(ArrayList fishies) {
fishies.remove(this);
}
}
Any solutions?
1