Trying to create a gravity system but keep getting NullPointerException and can't find the error...
in
Programming Questions
•
3 months ago
Hey,
Can anyone help me understand why I'm getting a NullPointerException?
I'm new to this and from what I understand it means I didn't initialize something.. I just can't seem to find what..
I'm trying to learn create a gravitational system, here is my code:
float G_CONSTANT = 6.674;
Planet moon,earth;
void setup(){
size(500,500);
moon = new Planet(100,100,10);
earth = new Planet(300,300,30);
}
void draw(){
moon.display();
moon.gravitationPull(earth);
earth.display();
earth.gravitationPull(moon);
}
class Planet {
int x,y;
float radius = 0;
float mass;
PVector location, velocity, acceleration,gravitation;
Planet(int x_, int y_, float radius_){
x = x_;
y = y_;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
mass = PI * sq(radius);
}
void display() {
noStroke();
fill(0);
ellipse(x,y,radius*2,radius*2);
}
void gravitationPull(Planet planet_) {
PVector dist = PVector.sub(planet_.location,this.location);
float GmDividedbyMag= ((-G_CONSTANT*planet_.mass)/dist.mag());
PVector gravitation = PVector.mult(dist,GmDividedbyMag);
acceleration = planet_.gravitation;
acceleration.div(planet_.mass);
velocity.add(acceleration);
location.add(velocity);
}
}
1