# Storing functions in array or arraylist returns null pointer

**URL:** <https://discourse.processing.org/t/storing-functions-in-array-or-arraylist-returns-null-pointer/13749>\
**Category:** Coding Questions\
**Created:** [September 1, 2019, 5:37pm UTC](https://discourse.processing.org/t/storing-functions-in-array-or-arraylist-returns-null-pointer/13749 "2019-09-01T17:37:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [September 1, 2019, 5:37pm UTC](https://discourse.processing.org/t/storing-functions-in-array-or-arraylist-returns-null-pointer/13749/1 "2019-09-01T17:37:16Z")

</div>

I’m converting code from p5 to processing. I was storing functions in an array. These functions create the buttons which I’m using in my application and allowed me to loop through the array to draw and manipulate the buttons as opposed to having to call each one individually.

Now calling them individually outside of the array works in processing, however I get a null pointer exception when I try to loop through the functions Ive stored in the array. Cant seem to find any documentation about this, so I’m just wandering if it is in fact possible.

```auto
Button [] buttons = {game,paths,switches,pathreset,pathfind,pathfind2,pathfind3,pathfind4,cluster,set_target,set_pathfinder,extract_circuit,back,reset};

```

```auto
for (int i=0;i<buttons.length;i++){
      Button a = buttons[i];
      a.draw();
      a.logic();
    }

```

```auto
class Button{
  HashMap<String,Integer> hm = new HashMap<String,Integer>();
  float x,y,w,h,value,ts;
  color col,col2;
  String label;
  int toggle,id,stroke;
  boolean on = false;
  Button(float X,float Y,float w_,float h_, String Label){
    
    x = X;
    y = Y;
    w = w_;
    h = h_;
    col = color(120);
    label = Label;
    toggle = 0;
    ts = 12;
  }
  
  void draw (){
   
    fill(col);
    noStroke();
    rect(x,y,w,h);
    fill(0);
    textSize(ts);
    text(label,x+10,y+h-10);
  }
  
  void logic(){
    if(toggle==1){
      this.col = color(255,255,255,200);
      this.stroke = 0;
      this.col2 = color(0,0,0,175);
    }
    else{
      this.stroke = 0;
      this.col2 = color(255,255,255,175);
      this.col = color(255,255,255,80);
    }
    
    if(toggle==2){
      toggle=0;
    }
  };

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [September 1, 2019, 7:52pm UTC](https://discourse.processing.org/t/storing-functions-in-array-or-arraylist-returns-null-pointer/13749/2 "2019-09-01T19:52:44Z")

</div>

Maybe first code of [this](https://forum.processing.org/two/discussion/10993/array-of-buttons) link helps.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [September 1, 2019, 8:03pm UTC](https://discourse.processing.org/t/storing-functions-in-array-or-arraylist-returns-null-pointer/13749/3 "2019-09-01T20:03:28Z")

</div>

Fixed. Processing requires the programmer to define the functions in the arrays directly (or so I’m assuming, because this solved my issue).  
I was doing this;

```auto
Button menu;
menu = new Button(menuxy[0],menuxy[1],float(190),float(25),0);
Button [] buttons = {menu};

```

and this did not work as it did not allow me to iterate through the array afterwards and resulted in a null pointer exception.

I did this instead;

```auto
Button buttons = new Button[1];
buttons [0] = new Button(menuxy[0],menuxy[1],float(190),float(25),0);
Button menu = button[0];

```

now I can loop through the array without any issue.
