# Syntax help with 2D array

**URL:** <https://discourse.processing.org/t/syntax-help-with-2d-array/45242>\
**Category:** Beginners\
**Created:** [October 29, 2024, 8:51am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242 "2024-10-29T08:51:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 29, 2024, 8:51am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/1 "2024-10-29T08:51:34Z")

</div>

I am trying to define a 2D array in Processing but I don’t seem to be able to get the syntax right.

I have defined the global variable

```auto
byte[][] chordBank2;

```

And in the setup function I have put

```auto
chordBank2 = new byte[5][7];

```

Then I want to put a function in to define my array

This is what I put

```auto
void defBank(){ // define array
  chordBank2[5][7] = {

                        {40, 45, 50, 55, 59, 64}; // open strings 0
                        {40, 47, 52, 56, 59, 64}; // E major 1
             { 41 ,48 , 53, 57, 60, 65}; // F major 2                   
             { 43, 47, 50, 55, 59, 67}; // G major 3
             { 33 , 45, 52, 57, 61, 64}; // A major 4
              {35 , 47, 54, 59, 63, 66}; // B major 5
             { 36 , 48, 52, 55, 60, 64}; // C major 6
             { 38 ,38 , 50, 57, 62, 66}; // D major 7
  }
}

```

But I keep getting

Syntax Error - Missing operator, semicolon, or ‘}’ near ‘{’?

I am unsure of which way round columns and rows go so I have swapped the 5 an 7 over but still no joy.

Can any one point me in the right direction to solving this.

Thanks for reading.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 29, 2024, 10:52am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/2 "2024-10-29T10:52:01Z")

</div>

Hello @Grumpy_Mike,

There is an example here that may help to initialize the array:

> **[Java Multidimensional Array (2d and 3d Array)](https://www.programiz.com/java-programming/multidimensional-array)**
>
> In this tutorial, we will learn about the Java multidimensional array using 2-dimensional arrays and 3-dimensional arrays with the help of examples. A multidimensional array is an array of arrays

_Updated_

Adding link below for any visitors to this topic in future.

There are tutorials here (including 2D arrays) that are a good starting point:  
_[Tutorials / Processing.org](https://processing.org/tutorials)_

`:)`

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [October 29, 2024, 11:31am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/3 "2024-10-29T11:31:04Z")

</div>

I’m not convinced that you can use the `{ ... }` initializer syntax directly within that function as you do. Looking into that article @glv posted should clear things up. Alternatively, you could assign each note array to it’s respective chord individually. Something like this:

`chordBank2[0] = new byte[] {40, 45, 50, 55, 59, 64}; // open strings`

And then repeat for all other chords. I feel this also keeps it somewhat cleaner and more structured.

Another big issue that jumps out is the whole “arrays start indexing at 0” thing that keeps tripping people up. You seem to be aware of this, though I believe you overcorrected in this particular case.

As I understand it, you want 8 chords, with 6 notes each. These numbers represent the _lenghts_ of the arrays you want to use. When you initialise arrays, you have to think in length, not indexes. Hence, your initialization `chordBank2 = new byte[7][5];` actually creates a 2D array of lengths 7 and 5. The whole thing flips when you want to access an array. Here you need to think with “starts at 0”.

Example:  
A 1D array of length 8 would be initialized with `exampleArray = new int[8]`.  
But if you want to access the last entry in that array, you use `exampleArray[7]`.

---

<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:** [October 29, 2024, 11:48am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/4 "2024-10-29T11:48:57Z")

</div>

> [@Grumpy\_Mike](#):
>
> Then I want to put a function in to define my array…

If that’s data that won’t change during the course of your sketch, you can simply declare it & initialize it at 1 single statement; no need for a separate function:

```auto
static final byte[][] CHORD_BANK2 = {
  { 40, 45, 50, 55, 59, 64 }, // open strings 0
  { 40, 47, 52, 56, 59, 64 }, // E major 1
  { 41, 48, 53, 57, 60, 65 }, // F major 2                   
  { 43, 47, 50, 55, 59, 67 }, // G major 3
  { 33, 45, 52, 57, 61, 64 }, // A major 4
  { 35, 47, 54, 59, 63, 66 }, // B major 5
  { 36, 48, 52, 55, 60, 64 }, // C major 6
  { 38, 38, 50, 57, 62, 66 } // D major 7
};

```

The variable name CHORD\_BANK2 is using the SCREAMING\_SNAKE\_CASE convention, hinting that its content shouldn’t ever be mutated.

P.S.: If you happen to have multiple chord banks, you can define a 3d-array instead for all of them:

```java
static final byte[][][] CHORD_BANKS = {
  { // bank 0
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major 1
    { 41, 48, 53, 57, 60, 65 }, // F major 2                   
    { 43, 47, 50, 55, 59, 67 }, // G major 3
    { 33, 45, 52, 57, 61, 64 }, // A major 4
    { 35, 47, 54, 59, 63, 66 }, // B major 5
    { 36, 48, 52, 55, 60, 64 }, // C major 6
    { 38, 38, 50, 57, 62, 66 } // D major 7
  },

  { // bank 1
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major 1
    { 41, 48, 53, 57, 60, 65 }, // F major 2                   
    { 43, 47, 50, 55, 59, 67 }, // G major 3
    { 33, 45, 52, 57, 61, 64 }, // A major 4
    { 35, 47, 54, 59, 63, 66 }, // B major 5
    { 36, 48, 52, 55, 60, 64 }, // C major 6
    { 38, 38, 50, 57, 62, 66 } // D major 7
  },

  { // bank 2
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major 1
    { 41, 48, 53, 57, 60, 65 }, // F major 2                   
    { 43, 47, 50, 55, 59, 67 }, // G major 3
    { 33, 45, 52, 57, 61, 64 }, // A major 4
    { 35, 47, 54, 59, 63, 66 }, // B major 5
    { 36, 48, 52, 55, 60, 64 }, // C major 6
    { 38, 38, 50, 57, 62, 66 } // D major 7
  }
};

```

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 30, 2024, 6:29am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/5 "2024-10-30T06:29:48Z")

</div>

Thanks, that looks very promising and was the sort of solution I was looking for.

However, I can’t seem to make it work, that is I can’t seem to actually access the values in the table.  
I needed to keep the

```auto
chord_Bank2 = new byte[5][7];

```

in the setup function otherwise I get a null pointer exception when trying to access the data.  
I wrote some code to print out the data:-

```auto
void printChord(int chord){ // print a chord
    for(int i=0; i<6; i++){
    print(" ",chord_Bank2[chord][i]);
  }
  println();

}

```

but all it ever returns is 0 for each note

> [@GoToLoop](#):
>
> The variable name CHORD\_BANK2 is using the SCREAMING\_SNAKE\_CASE convention, hinting that its content shouldn’t ever be mutated.

Yes this is the case that the table is never altered.  
Is there anything else I need to include to make this work?  
Thanks  
Mike

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [October 30, 2024, 10:12am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/6 "2024-10-30T10:12:32Z")

</div>

Can you paste your code which is throwing the nullpointer exception? It may be that you’ve got your chord/notes order reversed in the array.

Just putting all the discussed parts together works fine for me.

The @GoToLoop approach:

```auto
static final byte[][] chordBank2 = {
  { 40, 45, 50, 55, 59, 64 }, // open strings 0
  { 40, 47, 52, 56, 59, 64 }, // E major 1
  { 41, 48, 53, 57, 60, 65 }, // F major 2                   
  { 43, 47, 50, 55, 59, 67 }, // G major 3
  { 33, 45, 52, 57, 61, 64 }, // A major 4
  { 35, 47, 54, 59, 63, 66 }, // B major 5
  { 36, 48, 52, 55, 60, 64 }, // C major 6
  { 38, 38, 50, 57, 62, 66 } // D major 7
};

void setup() {
}

void draw() {
  printChord(2); // prints chord 2 
}

void printChord(int chord){ // print a chord
    for(int i=0; i<6; i++){
    print(" ",chordBank2[chord][i]);
  }
  println();
}

```

My approach:

```auto
byte[][] chordBank2 = new byte[8][6];

void setup() {  
  chordBank2[0] = new byte[] {40, 45, 50, 55, 59, 64}; // open strings
  chordBank2[1] = new byte[] {40, 47, 52, 56, 59, 64}; // E major
  chordBank2[2] = new byte[] {41, 48, 53, 57, 60, 65}; // F major
  chordBank2[3] = new byte[] {43, 47, 50, 55, 59, 67}; // G major
  chordBank2[4] = new byte[] {33, 45, 52, 57, 61, 64}; // A major
  chordBank2[5] = new byte[] {35, 47, 54, 59, 63, 66}; // B major
  chordBank2[6] = new byte[] {36, 48, 52, 55, 60, 64}; // C major
  chordBank2[7] = new byte[] {38, 50, 57, 62, 66, 69}; // D major 
}

void draw() {
  printChord(2); // prints chord 2 
}

void printChord(int chord){ // print a chord
    for(int i=0; i<6; i++){
    print(" ",chordBank2[chord][i]);
  }
  println();
}

```

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 30, 2024, 10:32am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/7 "2024-10-30T10:32:35Z")

</div>

> [@eightohnine](#):
>
> Can you paste your code which is throwing the nullpointer exception

It is simply caused by removing the

```auto
chord_Bank2 = new byte[5][7];`

```

in the setup function.

> [@eightohnine](#):
>
> It may be that you’ve got your chord/notes order reversed in the array.

No, when I revers them I get an array overrun message as well as the returned zeros.

It is worth noting that I do not want to just print out the chord but access the individual notes to pass on to something else.

Can I just add that I a fine programming in C/C++ but the way thing happen in Processing is often different.

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [October 30, 2024, 10:52am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/8 "2024-10-30T10:52:55Z")

</div>

> [@Grumpy\_Mike](#):
>
> It is worth noting that I do not want to just print out the chord but access the individual notes to pass on to something else.

Two options:  
You can access the 2D array directly. But then you need to compensate for the “starts indexing at 0” thing.  
Or you use a helper function and pass in what positions you want, and the helper function compensates for the indexing.

```auto
byte[][] chordBank2 = new byte[8][6];

void setup() {  
  chordBank2[0] = new byte[] {40, 45, 50, 55, 59, 64}; // open strings
  chordBank2[1] = new byte[] {40, 47, 52, 56, 59, 64}; // E major
  chordBank2[2] = new byte[] {41, 48, 53, 57, 60, 65}; // F major
  chordBank2[3] = new byte[] {43, 47, 50, 55, 59, 67}; // G major
  chordBank2[4] = new byte[] {33, 45, 52, 57, 61, 64}; // A major
  chordBank2[5] = new byte[] {35, 47, 54, 59, 63, 66}; // B major
  chordBank2[6] = new byte[] {36, 48, 52, 55, 60, 64}; // C major
  chordBank2[7] = new byte[] {38, 50, 57, 62, 66, 69}; // D major 
}

void draw() {  
  // access 2D array directly, need to think in indexes
  int aSingleNote = chordBank2[6][3]; // from the 7th chord (6th index), get the 4th note (3rd index)
  println("Direct note get: "+ aSingleNote); 
  
  // via a function
  int anotherSingleNote = getNote(7,4); // from the 7th chord, get the 4th note
  println("Function note get: "+anotherSingleNote);   
   
  stop();
}

int getNote(int chordNum, int noteNum) {
  return chordBank2[chordNum-1][noteNum-1];
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [October 30, 2024, 12:10pm UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/9 "2024-10-30T12:10:10Z")

</div>

Although the approaches by @GoToLoop and @eightohnine are equally valid I prefer the former because it is more concise although I would remove the `static` keyword because it has no benefit in this context i.e. a basic Processing sketch.

@eightohnine does show clearly the structure of a 2D array - _ **a 2D array is a 1D array of 1D arrays** _ and we can extend that to _ **a 3D array is a 1D array of 2D arrays** _

The following sketch combines some of the features of previous posts but also uses method / function overloading to improve flexibility in accessing chords and notes. Just in case you don’t know function overloading is where you have two or more functions with the same name but with different parameters. When you call the function the compiler looks ate the parameters you pass and selects the matching function for you.

```auto
final byte[][][] CHORD_BANKS = {
  { // bank 0
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major 1
    { 41, 48, 53, 57, 60, 65 }, // F major 2
    { 43, 47, 50, 55, 59, 67 }, // G major 3
    { 33, 45, 52, 57, 61, 64 }, // A major 4
    { 35, 47, 54, 59, 63, 66 }, // B major 5
    { 36, 48, 52, 55, 60, 64 }, // C major 6
    { 38, 38, 50, 57, 62, 66 } // D major 7
  },

  { // bank 1
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major 1
    { 41, 48, 53, 57, 60, 65 }, // F major 2
    { 43, 47, 50, 55, 59, 67 }, // G major 3
    { 33, 45, 52, 57, 61, 64 }, // A major 4
    { 35, 47, 54, 59, 63, 66 }, // B major 5
    { 36, 48, 52, 55, 60, 64 }, // C major 6
    { 38, 38, 50, 57, 62, 66 } // D major 7
  },

  { // bank 2
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major 1
    { 41, 48, 53, 57, 60, 65 }, // F major 2
    { 43, 47, 50, 55, 59, 67 }, // G major 3
    { 33, 45, 52, 57, 61, 64 }, // A major 4
    { 35, 47, 54, 59, 63, 66 }, // B major 5
    { 36, 48, 52, 55, 60, 64 }, // C major 6
    { 38, 38, 50, 57, 62, 66 } // D major 7
  }
};

void setup() {
  byte[][] bank = getBank(2);
  byte[] chord = getChord(bank, 3);
  printBank(bank);
  printChord(chord);
  println("Note : " + getNote(0, 3, 2));
}

byte[][] getBank(int bankNbr) {
  return CHORD_BANKS[bankNbr];
}

byte[] getChord(byte[][] bank, int chord) {
  return bank[chord];
}

byte[] getChord(int bankNbr, int chord) {
  return CHORD_BANKS[bankNbr][chord];
}

byte getNote( byte[] chord, int n) {
  return chord[n];
}

byte getNote(int bankNbr, int chord, int n) {
  return CHORD_BANKS[bankNbr][chord][n];
}

void printBank(byte[][] bank) {
  println("Bank : ");
  for (int chordIdx = 0; chordIdx < bank.length; chordIdx++) {
    byte[] chord = bank[chordIdx];
    print("Chord: " + chordIdx + " Notes: " );
    for (int noteIdx = 0; noteIdx < chord.length; noteIdx++) {
      print(" " + chord[noteIdx]);
    }
    println();
  }
}

void printBank(int bankNbr) {
  println("Bank munber : " + bankNbr);
  byte[][] bank = CHORD_BANKS[bankNbr];
  for (int chordIdx = 0; chordIdx < bank.length; chordIdx++) {
    byte[] chord = bank[chordIdx];
    print("Chord: " + chordIdx + " Notes: " );
    for (int noteIdx = 0; noteIdx < chord.length; noteIdx++) {
      print(" " + chord[noteIdx]);
    }
    println();
  }
}

void printChord(byte[] chord) {
  println("Chord : ");
  print(" Notes: " );
  for (int noteIdx = 0; noteIdx < chord.length; noteIdx++) {
    print(" " + chord[noteIdx]);
  }
  println();
}

void printChord(int bankNbr, int chordNbr) {
  println("Bank munber : " + bankNbr + " Chord number : " + chordNbr);
  byte[] chord = CHORD_BANKS[bankNbr][chordNbr];
  print(" Notes: " );
  for (int noteIdx = 0; noteIdx < chord.length; noteIdx++) {
    print(" " + chord[noteIdx]);
  }
  println();
}

```

---

<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:** [October 30, 2024, 1:30pm UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/10 "2024-10-30T13:30:46Z")

</div>

> [@quark](#):
>
> … although I would remove the `static` keyword because it has no benefit in this context i.e. a basic Processing sketch.

Neither `static` nor `final` are actually required for any code to run!

But when defining data which shouldn’t be mutated (regardless it’s enforced or not by the compiler), it’s pretty common to pair `static final` together, as we can see in this excerpt from Processing’s own source code:

> <https://github.com/processing/processing/blob/master/core/src/processing/core/PConstants.java#L86-L88>

Although I’d rename that array to PLATFORM\_NAMES, which is the appropriate naming convention for read-only data.

Although Java arrays are mutable, when we see at least 2 of these elements together (`static`, `final`, SCREAMING\_SNAKE\_CASE), those’d be a telltale sign we should only read such data, but never modify it.

Actually, the SCREAMING\_SNAKE\_CASE naming convention alone means immutability for any programming language.

Keyword `final` makes the compiler disallow a variable/parameter/field to be re-assigned to another value later:

> **[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…

However, the keyword `final` in Java doesn’t protect an object from being mutated, only its variable from being re-assigned!

Keyword `static`, when applied to a field or a “global” variable, it enforces its associated data to be created a single time only, regardless how many times its `class` might be instantiated later:

> **[static / Reference](https://processing.org/reference/static.html)**
>
> Keyword used to define a variable as a "class variable" and a method as a "class method." When a variable is declared with the static keyword, all instances of that class share the same variabl…

Although when using the PDE (Processing’s IDE), it’s not easy to create multiple instances of a sketch running all at once at the same time, it’s still a good convention practice to add `static` when defining an immutable data, together w/ `final` and SCREAMING\_SNAKE\_CASE.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 30, 2024, 2:54pm UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/11 "2024-10-30T14:54:06Z")

</div>

I do prefer the solution from you.  
This is just the first 7 lines of a data base that will extend, in the end, to 43 entries. I just used the first 7 to simplify matters.

Then could you explain why I can’t read the contents of the array, an only read back six zeros, please.

The solution from @eightohnine does work but this makes the setup function look very turgid when extended to 43 entries.

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [October 30, 2024, 3:11pm UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/12 "2024-10-30T15:11:30Z")

</div>

Wrap the whole bank/chord/note assignment into a function and call it in setup(). And park that long-ass function at the end of the sketch.

If you are using the Processing IDE, then you can even put that function into a separate tab.

```auto
byte[][] chordBank2 = new byte[8][6];

void setup() {  
  initializeBanks(); 
}

void draw() {  
  int anotherSingleNote = getNote(7,4); // from the 7th chord, get the 4th note
  println(anotherSingleNote);
  stop();
}

int getNote(int chordNum, int noteNum) {
  return chordBank2[chordNum-1][noteNum-1];
}

void initializeBanks() { 
  chordBank2[0] = new byte[] {40, 45, 50, 55, 59, 64}; // open strings
  chordBank2[1] = new byte[] {40, 47, 52, 56, 59, 64}; // E major
  chordBank2[2] = new byte[] {41, 48, 53, 57, 60, 65}; // F major
  chordBank2[3] = new byte[] {43, 47, 50, 55, 59, 67}; // G major
  chordBank2[4] = new byte[] {33, 45, 52, 57, 61, 64}; // A major
  chordBank2[5] = new byte[] {35, 47, 54, 59, 63, 66}; // B major
  chordBank2[6] = new byte[] {36, 48, 52, 55, 60, 64}; // C major
  chordBank2[7] = new byte[] {38, 50, 57, 62, 66, 69}; // D major 
  
  // add all other banks/chords/notes here... 
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [October 30, 2024, 4:03pm UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/13 "2024-10-30T16:03:27Z")

</div>

> [@GoToLoop](#):
>
> it’s pretty common to pair `static final` together, as we can see in this excerpt from Processing’s own source code:

There is a massive semantic difference between `static` and `final` as the references you provide point out.

Now even beginners can grasp the concept of variables and constants and it is good programming practice to mark constants with the appropriate keyword (in Java `final`) and to use use SCREAMING\_SNAKE\_CASE for the identifier name. Identifying constants in the code can help reduce the risk of logic errors so is a good habit to get into, even for beginners.

On the other hand `static` is totally incomprehensible concept to anyone who is unfamiliar with object orientated programming. For that reason I wouldn’t use it in a basic Processing sketch unless there is a _ **real need** _ to do so.

In this sketch there is no significant difference between using

`static final byte[][][] CHORD_BANKS = { ...`  
and  
`final byte[][][] CHORD_BANKS = { ...`

So its a matter of personal choice and my stating a preference was not meant as a criticism of your choice.

---

<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:** [October 30, 2024, 4:14pm UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/14 "2024-10-30T16:14:50Z")

</div>

> [@Grumpy\_Mike](#):
>
> Then could you explain why I can’t read the contents of the array,

1 possible reason is when we declare a local variable or parameter w/ the same name as a field or “global” variable.

When that happens it’s said the local variable is overshadowing the “global” variable.

Make sure you’re not re-declaring existing “global” variables!

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 31, 2024, 7:49am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/15 "2024-10-31T07:49:01Z")

</div>

> [@GoToLoop](#):
>
> 1 possible reason is when we declare a local variable or parameter w/ the same name as a field or “global” variable.
> 
> When that happens it’s said the local variable is overshadowing the “global” variable.
> 
> Make sure you’re not re-declaring existing “global” variables!

This is the exact code I am using can you spot where this is wrong please:-

```auto
byte[][] chord_Bank2;
static final byte[][] CHORD_BANK2 = {
  { 40, 45, 50, 55, 59, 64 }, // open strings 0
  { 40, 47, 52, 56, 59, 64 }, // E major 1
  { 41, 48, 53, 57, 60, 65 }, // F major 2                   
  { 43, 47, 50, 55, 59, 67 }, // G major 3
  { 33, 45, 52, 57, 61, 64 }, // A major 4
  { 35, 47, 54, 59, 63, 66 }, // B major 5
  { 36, 48, 52, 55, 60, 64 }, // C major 6
  { 38, 38, 50, 57, 62, 66 } // D major 7
};

void setup(){
  size(100, 100);
  chord_Bank2 = new byte[5][7]; 
  noLoop(); // Run once and stop
}

void draw(){
  background(125);
  printoutChord(2);
}

void printoutChord(int chord){ // print a chord    
for(int i=0; i<6; i++){    
print(" ",chord_Bank2[chord][i]); 
}
 println();

}

```

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [October 31, 2024, 8:21am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/16 "2024-10-31T08:21:29Z")

</div>

You’ve pasted together lines from different sources without making sure that variable names are adapted.

Processing is case-sensitive, so it interprets chord\_Bank2 and CHORD\_BANK2 as two separate things.

First, you’re creating chord\_Bank2 but never filling it. But you’re trying to access its contents and that will lead to an error (indexOutOfBounds or nullPointer).  
You’re also creating and filling CHORD\_BANK2 but then are never accessing any of its contents.

Pick one variable name and run with it.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)\
**Post date:** [October 31, 2024, 8:31am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/17 "2024-10-31T08:31:48Z")

</div>

> [@eightohnine](#):
>
> You’ve pasted together lines from different sources without making sure that variable names are adapted.

Doh!  
Rookie mistake how embarrassing. Thanks for pointing that out. Functions correctly now. Who do you think sould get the solution?

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [October 31, 2024, 8:37am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/18 "2024-10-31T08:37:09Z")

</div>

Not seeing the forest for the trees… happens to me every now and then.

I’ll leave you with the heavy, heavy burden of solving this thread. 😉

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [October 31, 2024, 9:39am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/19 "2024-10-31T09:39:05Z")

</div>

Just to clarify your code

```auto
byte[][] chord_Bank2;

```

_ **declares** _ a variable called `chord_Bank2` but does not _ **define** _ its contents. At this stage the variable `chord_Bank2` has the value `null`

```auto
static final byte[][] CHORD_BANK2 = {
  { 40, 45, 50, 55, 59, 64 }, // open strings 0

```

This statement not only _ **declares** _ a 2D byte array it also _ **defines** _ the array contents.

Note both these statements are executed before the `setup` method is called.

```auto
chord_Bank2 = new byte[5][7];

```

This statements _ **defines** _ the contents of a previously _ **declared** _ array. Since the array elements are of type `byte` (a Java primitive data type) then all the elements take on the default value of zero which is why you got 0’s printed out.

You could change the statement to

```auto
chord_Bank2 = CHORD_BANK2;

```

and it would use the values in `CHORD_BANK2`.

One word of warning, using the `final` keyword with `CHORD_BANK2` will prevent later statements like

```auto
CHORD_BANK2 = new byte[5][7];

```

being compiled but it does not prevent changes to values stored inside the array. In these statements

```auto
chord_Bank2 = CHORD_BANK2; // both reference the SAME array
chord_Bank2[2][3] = 999; // will also change the value in `CHORD_BANK2`

```

After these statements are executed

```auto
println(CHORD_BANK2[2][3]); 

```

will display `999`

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [October 31, 2024, 10:38am UTC](https://discourse.processing.org/t/syntax-help-with-2d-array/45242/20 "2024-10-31T10:38:04Z")

</div>

Great and detailed write-up here, I learned a lot from it. Especially the explanation of why 0s are created. I have no experience using the _byte_ type.

[Next page](https://discourse.processing.org/t/syntax-help-with-2d-array/45242.md?page=2)
