# NullPointerException by creating an object

**URL:** <https://discourse.processing.org/t/nullpointerexception-by-creating-an-object/39876>\
**Category:** Beginners\
**Created:** [November 25, 2022, 9:22pm UTC](https://discourse.processing.org/t/nullpointerexception-by-creating-an-object/39876 "2022-11-25T21:22:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Exartyss](https://avatars.discourse-cdn.com/v4/letter/e/e19b73/32.png) [@Exartyss](https://discourse.processing.org/u/Exartyss)\
**Post date:** [November 25, 2022, 9:22pm UTC](https://discourse.processing.org/t/nullpointerexception-by-creating-an-object/39876/1 "2022-11-25T21:22:47Z")

</div>

Hello !  
I wrote a program in which I created a class called Ro with objects : circles who goes on a random X position  
But by running the program it craches. It writes " NullPointerException" and  
“c[i++] = new Ro(width_1/10,30+i_60);” is highlighted.

The program ran once, I modified it and it don’t worked again so I deleted my modification with ctrl Z but it doesn’t worked.

Can you help me ?

```auto
  String nb_pop = "10";
  int tempo = 0;
  boolean start = false;
  boolean select = false;
  boolean newstart = false;
  int index;
  
  Ro[] c; 
  
  void setup(){
   size(1000,600);
  
  }
  
  class Ro{
    float x;
    float y;
    
    Ro(float x, float y){
      this.y = y;
      this.x = x;
    }
    void show(){
      fill(0);
      ellipse(x,y,30,30);
      println(x);
      println(y);
    }
    void run(){
      x += random(1,width*8/10);
    }
  }
  
  
  void draw(){
    background(255);
    rectMode(CENTER);
    textAlign(CENTER,CENTER); strokeWeight(4);
    fill(111,222,111,111);
    rect(width/2,height*3/4,width/6,height/10,width/32);
    textSize(30);
    fill(0);
    text("Start",width/2,height*3/4);
    text(nb_pop,width/2,height*2/5);
  
      tempo +=1; 
      if(mousePressed){
        if(mouseY > height/2){
          start = true;
        }
      }
      if(start == true){
        background(255);
        line(width*3/4,0,width*3/4,height);
        for(int i=0; i < 10; i++){
          fill(255);
          c[i++] = new Ro(width*1/10,30+i*60);
          c[i++].run();
          c[i++].show();
        noLoop();
        }
        
      }
  }

Thanks
```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [November 25, 2022, 9:34pm UTC](https://discourse.processing.org/t/nullpointerexception-by-creating-an-object/39876/2 "2022-11-25T21:34:04Z")

</div>

Hi @Exartyss,

Your array `c` has currently a size of 0 elements.

> [@Exartyss](#):
>
> `Ro[] c; `

And you try to put more elements than capacity to it…

> [@Exartyss](#):
>
> ```auto
> for(int i=0; i < 10; i++){
> fill(255);
> c[i++] = new Ro(width*1/10,30+i*60);
> 
> ```

Short … You need to tell how much elements it should have …

Ie…

```auto
c = new Ro[10];

```

Cheers  
— mnse

> **[Array / Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

PS: also this will fail if you handle the index that way, as I increases on each step …

```auto
          // will crash
          c[i++] = new Ro(width*1/10,30+i*60);
          c[i++].run();
          c[i++].show();
         // should be
          c[i] = new Ro(width*1/10,30+i*60);
          c[i].run();
          c[i].show();

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 25, 2022, 9:40pm UTC](https://discourse.processing.org/t/nullpointerexception-by-creating-an-object/39876/3 "2022-11-25T21:40:04Z")

</div>

> [@mnse](#):
>
> Your array `c` has currently a size of 0 elements.

- Actually `Ro[] c;` merely creates a variable of type Ro[].
- But the array doesn’t exist yet!
- If you know how many Ro[] objects you’re gonna need, it’s better to create the array at the same time you’re declaring its variable: `Ro[] c = new Ro[10];`

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [November 25, 2022, 9:58pm UTC](https://discourse.processing.org/t/nullpointerexception-by-creating-an-object/39876/4 "2022-11-25T21:58:29Z")

</div>

Hi @GoToLoop,

Sure! But thought it makes clearer what’s the issue in that case … 😉

> [@GoToLoop](#):
>
> > [@mnse](#):
> >
> > Your array `c` has currently a size of 0 elements.
> 
> - Actually `Ro[] c;` merely creates a variable of type Ro.

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Exartyss](https://avatars.discourse-cdn.com/v4/letter/e/e19b73/32.png) [@Exartyss](https://discourse.processing.org/u/Exartyss)\
**Post date:** [November 26, 2022, 7:35am UTC](https://discourse.processing.org/t/nullpointerexception-by-creating-an-object/39876/5 "2022-11-26T07:35:24Z")

</div>

Oky  
It works. Thanks a lot!  
you too @GoToLoop
