Loading 2D array of Strings in setup returns null


I’m working on a sketch where at some point I want to load a 2D String array with randomly generated ‘words’ (which are randomly generated strings of chars). I want to pass these on to an Object and reference and use them later in the sketch. After initializing and loading the 2D array I noticed that, when trying to reference Strings in the array, it would return a null for every given entry.

I managed to isolate the problem, currently the code that’s not working the way I intended it to looks like this:

String wordList[][];
int iMax, jMax;

void setup() {
  size(500, 500);

  iMax = 50;
  jMax = 20;

  for (int i = 0; i < iMax; i++) {
    for (int j = 0; j < jMax; j++) {
      String rWord = getAlphaString(20);
      wordList = new String[iMax][jMax];
      wordList[i][j] = rWord;
  println(wordList.length, wordList[0].length, wordList[0][10]);

void draw(){
  //wordList[0][0] = getAlphaString(10);

// function to generate a random string of length n
String getAlphaString(int n) {
  StringBuilder sb = new StringBuilder(n);

  for (int i = 0; i < n; i++) {
    int index = (int)(AlphaString.length() * Math.random());

  return sb.toString();

I’m not sure why this method in Setup seemingly doesn’t store the 2D array with a set of String data. When uncommenting the lines in Draw it seems to work out the way I intend it to.

Am I overseeing something? Am I loading the 2D-array in setup in an incorrect way? Is the function used to generate the String of text not suited for my goal? Any input is welcome, thanks a lot in advance with helping me!


You’re recreating the 2D array and re-assigning it to wordList[][] inside the double loop over & over:

In order to avoid accidental re-assignments we should use keyword final when declaring a variable:
final String[][] wordList[50][20];

Here’s your sketch modified to use final keyword:

static final int ROWS = 5, COLS = 3, LEN = 20;
final String[][] wordList = new String[ROWS][COLS];

void setup() {
  for (int r = 0; r < ROWS; ++r) for (int c = 0; c < COLS; ++c)
    wordList[r][c] = getAlphaString(LEN);

  for (final String[] row : wordList) println(row);


String getAlphaString(final int n) {
  final StringBuilder sb = new StringBuilder(n);
  for (int i = 0; i < n; ++i) sb.append((char) random('A', '['));
  return sb.toString();

Ah! Thank you GoToLoop!
So, if I understand you correctly, in my code the reason why the wordList[0][0] element returned a null, was because the 2D-array wordList was redeclared completely every time, therefore completely set to non-declared variables (returning a null if called upon), and only the ‘current’ element in the double for-loop was made the randomized String?

And is this a correct use of the keyword final? I always thought that final was used solely for making sure that a declared array/list/etc. would not accidentally be altered after being filled with variables. I didn’t know it could also be used to prevent reinitializing, but maybe I’m thinking in boxes too much. :nerd_face:

Recreated & reassigned, but not redeclared.

Keyword final seals a variable from being reassigned.
But it doesn’t affect the contents of an object a variable points to.

1 Like