# Loading 2D array of Strings in setup returns null

**URL:** <https://discourse.processing.org/t/loading-2d-array-of-strings-in-setup-returns-null/42434>\
**Category:** Beginners\
**Created:** [July 17, 2023, 6:29am UTC](https://discourse.processing.org/t/loading-2d-array-of-strings-in-setup-returns-null/42434 "2023-07-17T06:29:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Namrad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/namrad/32/15938_2.png) [@Namrad](https://discourse.processing.org/u/Namrad)\
**Post date:** [July 17, 2023, 6:29am UTC](https://discourse.processing.org/t/loading-2d-array-of-strings-in-setup-returns-null/42434/1 "2023-07-17T06:29:40Z")

</div>

Hello,

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:

```auto
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];
      println(rWord);
      wordList[i][j] = rWord;
      println(rWord);
      println(wordList[i][j]);
    }
  }
  println(wordList.length, wordList[0].length, wordList[0][10]);
}

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

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

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

  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!

Namrad

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 17, 2023, 7:38am UTC](https://discourse.processing.org/t/loading-2d-array-of-strings-in-setup-returns-null/42434/2 "2023-07-17T07:38:37Z")

</div>

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

> [@Namrad](#):
>
> ```auto
> for (int i = 0; i < iMax; i++) {
> for (int j = 0; j < jMax; j++) {
> String rWord = getAlphaString(20);
>       
> wordList = new String[iMax][jMax];
> 
> ```

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

> **[final / Reference](https://processing.org/reference/final.html)**
>
> Keyword used to state that a value, class, or method can't be changed. If the final keyword is used to define a variable, the variable can't be changed within the program. When used to define a…

Here’s your sketch modified to use `final` keyword:

```auto
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);

  exit();
}

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();
}

```

---

<div class="post-metadata">

**Author:** ![Namrad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/namrad/32/15938_2.png) [@Namrad](https://discourse.processing.org/u/Namrad)\
**Post date:** [July 17, 2023, 9:53am UTC](https://discourse.processing.org/t/loading-2d-array-of-strings-in-setup-returns-null/42434/3 "2023-07-17T09:53:48Z")

</div>

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. 🤓

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 17, 2023, 10:11am UTC](https://discourse.processing.org/t/loading-2d-array-of-strings-in-setup-returns-null/42434/4 "2023-07-17T10:11:18Z")

</div>

> [@Namrad](#):
>
> … was because the 2D-array _wordList_ was redeclared completely every time,

Recreated & reassigned, but not redeclared.

> [@Namrad](#):
>
> …for making sure that a declared array/list/etc. would not accidentally be altered…

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