Can't print IntList outside setup function

I can print listaA within setup function but not when I press a “w” key. Why?

IntList Ptop;
final int ROWS = 8;
final IntList[] listaA = new IntList[ROWS];
int value;
int index;

void setup() {
  size(520, 220);

  Ptop = new IntList();

  Ptop.append(new int[] {-1, -1, -1, -1, 5, 11, 8, 11, 7, 11, 6, 11, 5, 12, 9, 12, 3, 12, 2, 12, 0, -2, -2, -2, -2});
  println(Ptop);

  for (int value=6; value<=20; value=value+2) {
    index = 0;
    listaA[index] = new IntList();
    for (int i=value; i<Ptop.size(); i=i+1) {
      listaA[index].append(Ptop.get(i));
    }
    for (int i=value-2; i>=0; i=i-1) {
      listaA[index].append(Ptop.get(i));
    }
    println(listaA[index]);
    index++;
  }
}

void keyReleased() {
  if (key == 'w') {
    for (int k=0; k<8; k++) {
      println(listaA[k]);
    }
  }
}

Hi,

Welcome to the forum! :wink:

As @glv said, the Processing reference is the way to go if you have any questions concerning the functions and methods!

From the keyReleased() page :

Mouse and keyboard events only work when a program has draw() . Without draw() , the code is only run once and then stops listening for events.

1 Like

Yes @glv, sorry! :wink:

My joy of discovery is too strong ahah

When I add draw() function, I have the following problem:

Only one listaA is printed, others are null:

IntList size=24 [ 0, -2, -2, -2, -2, 2, 12, 3, 12, 9, 12, 5, 11, 6, 11, 7, 11, 8, 11, 5, -1, -1, -1, -1 ]
null
null
null
null
null
null
null

Hello,

for (int value=6; value<=20; value=value+2) 
    {
    index = 0;
    
   //Your code

    println(index, listaA[index]); // Added index so you can see which what code is doing.
    index++;
  }

For starters…

You are setting index to 0 at the start of each loop.
Your code is doing what you programmed it to do and that is why they are null after index = 0;

:)

2 Likes