Problem in coding can't understand it

so i coded this to allow me to make planets around the sun
but it gives me error

THE CODE:-

planet sun;
planet earth;
void setup(){
  size (1000,1000);
  sun= new planet(width/2,height/2,40,#FFBC13);
  earth=new planet(sun,0,400,10,#159CEE);
}
void draw(){
  background(0);
  sun.display();
  earth.display();
}

OTHER TAB:-

class planet{
    planet other;
  color average;
  float diameter;
    float distance;
    float speed;
  float x=other.x+cos(speed)*distance;
  float y=other.y+sin(speed)*distance;
  
  planet(float tempX,float tempY,float tempDiameter,color tempColor){
    x=tempX;
    y=tempY;
    diameter=tempDiameter;
    average=tempColor;
  }
  planet(planet tempOther,float tempSpeed,float tempDistance,float tempDiameter,color tempColor){
    other=tempOther;
    speed=tempSpeed;
    distance=tempDistance;
    diameter=tempDiameter;
    average=tempColor;
  }
  void display(){
    noStroke();
    fill(average);
    ellipse(x,y,diameter,diameter);
  }
  
}

when i execute it
it gives me this error

java.lang.NullPointerException: Attempt to read from field ‘float processing.test.planets2.Planets2$planet.x’ on a null object reference
at processing.test.planets2.Planets2$planet.(Planets2.java:37)
at processing.test.planets2.Planets2.setup(Planets2.java:23)
at processing.core.PApplet.handleDraw(PApplet.java:1801)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)
java.lang.NullPointerException: Attempt to read from field ‘float processing.test.planets2.Planets2$planet.x’ on a null object reference
at processing.test.planets2.Planets2$planet.(Planets2.java:37)
at processing.test.planets2.Planets2.setup(Planets2.java:23)
at processing.core.PApplet.handleDraw(PApplet.java:1801)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:471)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:503)

1 Like

:thinking: You get the error because here:

:bulb:You are trying to create a class with parameters that doesn’t exit yet.

Here you are a possible solution, also the code formatted, remember to format your code with the </> button please :smiley:

Example running
Planet sun,earth;

void setup(){
  size (1000,1000);
  sun= new Planet(width/2,height/2,40,#FFBC13);
  earth=new Planet(sun,0.01,400,10,#159CEE);
}

void draw(){
  background(0);
  sun.display();
  earth.display();
}

class Planet{
    Planet other;
    color average;
    float diameter;
    float distance;
    float speed;
    float x;//=other.x+cos(speed)*distance;
    float y;//=other.y+sin(speed)*distance;
  
  Planet(float tempX,float tempY,float tempDiameter,color tempColor){
    x=tempX;
    y=tempY;
    diameter=tempDiameter;
    average=tempColor;
  }
  
  Planet(Planet tempOther,float tempSpeed,float tempDistance,float tempDiameter,color tempColor){
    other=tempOther;
    speed=tempSpeed;
    distance=tempDistance;
    diameter=tempDiameter;
    average=tempColor;
  }
  
  void display(){
    noStroke();
    fill(average);
    ellipse(x,y,diameter,diameter);
    if(this.speed != 0){
      this.x = other.x+cos(speed)*distance;
      this.y =other.y+sin(speed)*distance;
      this.speed+=0.01;
    }
  }
}
4 Likes

can you explain it more please
i cant understand how they dont exist yet

planet other; //this is null
color average; //null (or maybe just 0)
float diameter; //0
float distance; //0
float speed; //0
float x=other.x+cos(speed)*distance; //this is doing this : 
//float x = null.x (which is nothing, not 0, nothing...) +... and the rest. 
//you are trying to access the x of a planet that you never defined
//defining means other = new Planet(...); or other = jupiter; but you didn’t do this yet
//and your first constructor never actually does that , only the second one. 
float y=other.y+sin(speed)*distance;

Also, @Waboqueox , i‘m not sure if the OP want the x and y to be updated, but maybe to only be relative to the other Planets first x and y when created.

class Planet{
    Planet other;
    color average;
    float diameter;
    float distance;
    float speed;
    float x;//=other.x+cos(speed)*distance;
    float y;//=other.y+sin(speed)*distance;
  
  Planet(float tempX,float tempY,float tempDiameter,color tempColor){
    x=tempX;
    y=tempY;
    diameter=tempDiameter;
    average=tempColor;
  }
  
  Planet(Planet tempOther,float tempSpeed,float tempDistance,float tempDiameter,color tempColor){
    other=tempOther;

    x=other.x+cos(speed)*distance;
    y=other.y+sin(speed)*distance;

    speed=tempSpeed;
    distance=tempDistance;
    diameter=tempDiameter;
    average=tempColor;
  }
  
  void display(){
    noStroke();
    fill(average);
    ellipse(x,y,diameter,diameter);
  }
}