# Problem in coding can't understand it

**URL:** https://discourse.processing.org/t/problem-in-coding-cant-understand-it/16363
**Category:** Coding Questions
**Created:** [December 13, 2019, 6:57am UTC](https://discourse.processing.org/t/problem-in-coding-cant-understand-it/16363 "2019-12-13T06:57:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![NourAdel205](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/nouradel205/32/7486_2.png) [@NourAdel205](https://discourse.processing.org/u/NourAdel205)
#### Post date: [December 13, 2019, 6:57am UTC](https://discourse.processing.org/t/problem-in-coding-cant-understand-it/16363/1 "2019-12-13T06:57:27Z")

</div>

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

THE CODE:-

```auto
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:-

```auto
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)

---

<div class="post-metadata">

### Author: ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)
#### Post date: [December 13, 2019, 7:58am UTC](https://discourse.processing.org/t/problem-in-coding-cant-understand-it/16363/2 "2019-12-13T07:58:44Z")

</div>

🤔 You get the error because here:

> [@NourAdel205](#):
>
> float x=other.x+cos(speed)\*distance;  
> float y=other.y+sin(speed)\*distance;

💡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 😃

> **Example running**
>
> ```auto
> 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;
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![NourAdel205](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/nouradel205/32/7486_2.png) [@NourAdel205](https://discourse.processing.org/u/NourAdel205)
#### Post date: [December 15, 2019, 6:04am UTC](https://discourse.processing.org/t/problem-in-coding-cant-understand-it/16363/3 "2019-12-15T06:04:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [December 15, 2019, 8:54am UTC](https://discourse.processing.org/t/problem-in-coding-cant-understand-it/16363/4 "2019-12-15T08:54:47Z")

</div>

```auto
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.

```auto
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);
  }
}

```
