# Fix NullPointerException error

**URL:** <https://discourse.processing.org/t/fix-nullpointerexception-error/30632>\
**Category:** Beginners\
**Created:** [June 10, 2021, 6:20pm UTC](https://discourse.processing.org/t/fix-nullpointerexception-error/30632 "2021-06-10T18:20:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chencho](https://avatars.discourse-cdn.com/v4/letter/c/ea5d25/32.png) [@Chencho](https://discourse.processing.org/u/Chencho)\
**Post date:** [June 10, 2021, 6:20pm UTC](https://discourse.processing.org/t/fix-nullpointerexception-error/30632/1 "2021-06-10T18:20:33Z")

</div>

I have an error in my code and I’m not sure how to fix it.I’m relatively new to Arrays.  
I want to sort the numbers given in Arreglo into two other arrays, one for positive numbers and the other for negative.

Error is on line 25

```auto
int N=4;
float [] pos;
float [] neg;

void setup(){
  float [] Arreglo = {-1, 5, -3, 2};
  println("Valores del Arreglo");
  println(Arreglo);
  
  posNeg(Arreglo);
  println("Numeros positivos y cero:");
  println(pos);
  println("Numeros negativos:");
  println(neg);
}

void posNeg (float [] Arreglo){
  for (int i=0; i<=N; i++){
    float comp = Arreglo[i]; 
    if (comp>=0){
      pos[i]=comp;
    }
    else{
      neg[i]=comp;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 10, 2021, 6:53pm UTC](https://discourse.processing.org/t/fix-nullpointerexception-error/30632/2 "2021-06-10T18:53:18Z")

</div>

you need to initialize the arrays pos and neg with empty slots (before setup)

and for loop must be with `< N` instead of `<= N`

Here is a running example BUT as you can see, pos and neg contain empty slots (0.0 between the correct values) - can you think of a way to improve this? maybe look at append in the array section of the reference.

Hey, and welcome to the forum, great to have you here!

Warm regards,

Chrisir

```auto
int N=4;
float [] pos=new float [4]; // new
float [] neg=new float [4];

void setup() {
  size(111, 111); 
  float [] Arreglo = {-1, 5, -3, 2};
  println("Valores del Arreglo");
  println(Arreglo);

  posNeg(Arreglo);
  println("Numeros positivos y cero:");
  println(pos);
  println("Numeros negativos:");
  println(neg);
}

void posNeg (float [] Arreglo) {
  //
  for (int i=0; i<N; i++) { // here 
    float comp = Arreglo[i]; 
    if (comp>=0) {
      pos[i]=comp;
    } else {
      neg[i]=comp;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chencho](https://avatars.discourse-cdn.com/v4/letter/c/ea5d25/32.png) [@Chencho](https://discourse.processing.org/u/Chencho)\
**Post date:** [June 10, 2021, 7:06pm UTC](https://discourse.processing.org/t/fix-nullpointerexception-error/30632/3 "2021-06-10T19:06:55Z")

</div>

Thanks!  
I did think of initializing the Array but found the same problem as you as the lenghtof the array isn’t defined until the last part and there could be no negative numbers for example.  
Thanks for the inputon why I was getting the error and the welcoming:)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 10, 2021, 7:21pm UTC](https://discourse.processing.org/t/fix-nullpointerexception-error/30632/4 "2021-06-10T19:21:13Z")

</div>

Yeah, start with empty arrays and use command append
