Storing functions in array or arraylist returns null pointer

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.

Button [] buttons = {game,paths,switches,pathreset,pathfind,pathfind2,pathfind3,pathfind4,cluster,set_target,set_pathfinder,extract_circuit,back,reset};
for (int i=0;i<buttons.length;i++){
      Button a = buttons[i];
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 (){
  void logic(){
      this.col = color(255,255,255,200);
      this.stroke = 0;
      this.col2 = color(0,0,0,175);
      this.stroke = 0;
      this.col2 = color(255,255,255,175);
      this.col = color(255,255,255,80);

Maybe first code of this link helps.

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;

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;

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.

1 Like