# Writing my first Class, and getting in trouble already

**URL:** <https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844>\
**Category:** Beginners\
**Created:** [September 18, 2022, 7:31pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844 "2022-09-18T19:31:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 18, 2022, 7:31pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/1 "2022-09-18T19:31:57Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

I have a need to make my objects in an array. I am getting Null Pointer already… What am I doing wrong?

Thanks

```auto

Blocks[] Bl = new Blocks[20];

void setup() {
  size(640, 360);
//Bl[1]={0,0,0,0,0};
  background(255);
  //noLoop();
}

void draw() {

Bl[1].Lower=33;
Bl[1].Duration=22;
Bl[1].ShowProperties();
  
}

class Blocks { int Lower, Duration, Start, AVG_RPM, State;
 Blocks (int Lower, int Duration, int Start, int AVG_RPM, int State) 
 {  
  } 
   void ShowProperties()
   { println ("Bl1: " +Lower, Duration, Start, AVG_RPM, State);
   
   }

} 

```

---

<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:** [September 18, 2022, 7:37pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/2 "2022-09-18T19:37:20Z")

</div>

Hello,

Resources:

- [https://processing.org/tutorials/objects](https://processing.org/tutorials/objects)
- [https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html](https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html)
- [https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception.html](https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception.html)

You need to initialize the array:

```auto
Blocks[] Bl = new Blocks[20];

void setup() 
  {
  size(640, 360);
  //Bl[1]={0,0,0,0,0};
  background(255);

  Bl[1] = new Blocks(1, 2, 3, 4, 5); // Initialize array with a new object
  }

```

Note: I only initialized one element of the array!

`:)`

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 18, 2022, 10:55pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/3 "2022-09-18T22:55:24Z")

</div>

thanks a lot. I will actually need a 2 dimentional array so i tried to do a for loop initialization.  
Gives me 'error in .class"  
I can initialize one by one ok.

```auto

Blocks[][] Bl = new Blocks[6][20];

void setup() {
  size(640, 360);
   for (int =0; i<=5; i++)
{Bl[i][1]=new Blocks(1,2,3,4,5);
}

Bl[1][1]=new Blocks(1,2,3,4,5);
  background(255);
  //noLoop();
}

void draw() {

Bl[1][1].ShowProperties();
  
}

class Blocks { int Lower, Duration, Start, AVG_RPM, State;
 Blocks (int Lower, int Duration, int Start, int AVG_RPM, int State) 
 {  
  } 
   void ShowProperties()
   { println ("Bl: " +Lower, Duration, Start, AVG_RPM, State);
   
   }

} 

```

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 19, 2022, 12:59am UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/5 "2022-09-19T00:59:19Z")

</div>

I guess I was doing it wrong. Here it works.

```auto
  for (int i= 0; i <=19; i++)
  {
    Bl[0][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[1][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[2][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[3][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[4][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[5][i]=new Blocks(0, 0, 0, 0, 0);
  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 19, 2022, 1:34am UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/6 "2022-09-19T01:34:12Z")

</div>

Please remember to click ctrl-t in processing to get auto format

Also please read the text tutorial about objects

In short, in the constructor of the class you need to  
copy the incoming parameters into the class variables

The parameters (what is inside the (…)) should have temp added to the names so that they are different from the class variables

Then say lower=lowerTemp; etc.

* * *

Arrays start with 0, not 1

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 19, 2022, 1:43am UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/7 "2022-09-19T01:43:35Z")

</div>

Got it, thanks.

My next problem is making “splice” work in a 2 dimensional array.  
I am imputing data all the time, and I need the new data to be always at index 0, and the previous data to shift down to index 1, 2, 3…  
I have been using splice successfully for that in a one dimension array. but now i get errors.  
Here is what I tried:

```auto

Blocks[][] Bl = new Blocks[6][20];
int num= 0;
String str_num = "";

void setup() {
  size(640, 360);
  for (int i= 0; i <=19; i++)
  {
    Bl[0][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[1][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[2][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[3][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[4][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[5][i]=new Blocks(0, 0, 0, 0, 0);
  }
  background(255);
  //noLoop();
}

void draw() {

    
  
}

class Blocks { int Lower, Duration, Start, AVG_RPM, State;
 Blocks (int Lower, int Duration, int Start, int AVG_RPM, int State) 
 {  
  } 
   void ShowProperties()
   { println ("Bl[1]: " +Lower, Duration, Start, AVG_RPM, State);
   
   }

} 

void keyPressed(){
  if( key >= '0' && key <= '9' ){
   str_num += key; 
  }
  if( key == ENTER || key == RETURN ){
    num = int( str_num );
    Bl[1][0].Duration=num;
    str_num = "";
        for (int i=0; i<=19; i++)
        {
          Bl[1][i].ShowProperties();
        }
        Bl[1] = splice (Bl[1], num, 0);
    //println( num );
  }
}

```

Type mismatch, “java.lang.Object” does not match with “2D\_Array.Blocks”

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 19, 2022, 7:09am UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/8 "2022-09-19T07:09:22Z")

</div>

> [@laptophead](#):
>
> `Bl[1] = splice (Bl[1], num, 0);`

`... = (Blocks[]) splice...`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 19, 2022, 12:46pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/9 "2022-09-19T12:46:10Z")

</div>

not fully sure what your intention is

and not fully sure whether you know what a 2D grid means

Here is my version

- where you can add your input (instead of splice I wrote my own function myConcat to splice a new line BEFORE the old lines) and
- where the screen shows the 2D grid…

```auto
final int maxColumns = 6;

Blocks[][] Bl = new Blocks[maxColumns][20];
String str_num = "";

// ----------------------------------------------------------------------------------------

void setup() {
  size(1740, 860);
  for (int i= 0; i < 20; i++) {
    Bl[0][i]=new Blocks(i, 0, 0, 0, 0);
    Bl[1][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[2][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[3][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[4][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[5][i]=new Blocks(0, i, 0, 0, 0);
  }
  background(255);
}

void draw() {    
  //
  background(255);

  showScreen();

  // Black 
  fill(9); 
  stroke(9);
  // show TABLE 
  for (int i2=0; i2<Bl.length; i2++) {
    for (int i=0; i<Bl[i2].length; i++) {
      text( "Bl " + i2 +", " + i +" : " 
        + Bl[i2][i].getProperties(), 
        19+260*i2, 39+29*i, 
        260, 133);
    }
  }// for
}//func 

// ----------------------------------------------------------------------------------------

void showScreen() {

  // Black 
  fill(9); 
  stroke(9);

  // Headline 
  text("2D-Table with rows and columns", 
    244, 19); 
  line( 24, 22, 
    width-55, 22);

  // show RED Input area  
  fill(255, 0, 0);
  stroke(255, 0, 0);
  text ("Enter: "+str_num, 
    200, height- 46);
  line (200, height- 62, 
    1200, height- 62);
}

void keyPressed() {
  if ( key >= '0' && key <= '9' ) {
    str_num += key;
  } else if ( key == ENTER || key == RETURN ) {
    int num = 0;
    num = int( str_num );

    // we init the new line 
    Blocks[][] Bl_new = new Blocks[maxColumns] [1];

    int i=0; 
    Bl_new[0][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[1][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[2][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[3][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[4][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[5][i]=new Blocks(0, -1, 0, 0, 0);

    Bl_new[1][i].Duration=num;
    str_num = "";
    //for (int i=0; i<=19; i++) {
    // Bl[1][i].ShowProperties();
    //}
    // Bl = (Blocks[][]) splice (Bl, Bl_new, 0);
    // Add new line at START of table
    Bl = myConcat (Bl_new, Bl );
  }
}

Blocks[][] myConcat( Blocks[][] newBl, 
  Blocks[][] oldBl ) {

  // Add new line at START of table

  println(oldBl.length+1); 

  Blocks[][] Bl_result = new Blocks[oldBl.length][oldBl[0].length+1];

  // init 1st line
  for (int i_row = 0; i_row < 6; i_row++) {
    Bl_result[i_row][0]=new Blocks(0, 0, 0, 0, 0);
    Bl_result[i_row][0]=newBl[i_row][0];
  }

  // init other lines
  for (int i_row=1; i_row<Bl_result[0].length; i_row++) {
    for (int i = 0; i < 6; i++) {
      Bl_result[i][i_row]=new Blocks(0, 0, 0, 0, 0);
      Bl_result[i][i_row]=oldBl[i][i_row-1];
    }
  }

  return Bl_result;
}

// =====================================================================================

class Blocks { 

  int Lower, Duration, Start, AVG_RPM, State;

  // constr
  Blocks (int LowerTemp, int DurationTemp, 
    int StartTemp, int AVG_RPMTemp, int StateTemp) {
    Lower=LowerTemp; 
    Duration=DurationTemp;
    Start=StartTemp; 
    AVG_RPM=AVG_RPMTemp;
    State=StateTemp;
  }// constr

  void ShowProperties() { 
    println ("Bl[1]: " +Lower, Duration, Start, AVG_RPM, State);
  }//method

  String getProperties() { 
    return
      Lower +", " 
      + Duration + ", "
      + Start + ", "
      + AVG_RPM + ", "
      + State;
  }//method
  //
}//class 
//

```

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 19, 2022, 3:46pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/10 "2022-09-19T15:46:30Z")

</div>

Yes,  
This works. I will integrate it in my big sketch.  
However I wonder if the array will increase forever as I add data?

I want to limit it to the last 20 entries.  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 19, 2022, 7:20pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/11 "2022-09-19T19:20:34Z")

</div>

```auto

final int maxColumns = 6;

Blocks[][] Bl = new Blocks[maxColumns][20];
String str_num = "";

boolean showCursorAsLine; 

// ----------------------------------------------------------------------------------------

void setup() {
  size(1740, 860);
  for (int i= 0; i < 20; i++) {
    Bl[0][i]=new Blocks(i, 0, 0, 0, 0);
    Bl[1][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[2][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[3][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[4][i]=new Blocks(0, 0, 0, 0, 0);
    Bl[5][i]=new Blocks(0, i, 0, 0, 0);
  }
  background(255);
}

void draw() {    
  //
  background(255);

  showScreen();

  // Black 
  fill(9); 
  stroke(9);
  // show TABLE 
  for (int i2=0; i2<Bl.length; i2++) {
    for (int i=0; i<Bl[i2].length; i++) {
      text( "Bl " + i2 +", " + i +" : " 
        + Bl[i2][i].getProperties(), 
        19+260*i2, 39+29*i, 
        260, 133);
    }
  }// for
}//func 

// ----------------------------------------------------------------------------------------

void showScreen() {

  // Black 
  fill(9); 
  stroke(9);

  // Headline 
  text("2D-Table with rows and columns", 
    244, 19); 
  line( 24, 22, 
    width-55, 22);

  // show RED Input area  
  fill(255, 0, 0);
  stroke(255, 0, 0);
  text ("Enter: "
    +str_num
    +blinkingCursor(), 
    200, height- 46);
  line (200, height- 62, 
    1200, height- 62);
}

void keyPressed() {
  if ( key >= '0' && key <= '9' ) {
    str_num += key;
  } else if ( key == ENTER || key == RETURN ) {
    int num = 0;
    num = int( str_num );

    // we init the new line 
    Blocks[][] Bl_new = new Blocks[maxColumns] [1];

    int i=0; 
    Bl_new[0][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[1][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[2][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[3][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[4][i]=new Blocks(0, 0, 0, 0, 0);
    Bl_new[5][i]=new Blocks(0, -1, 0, 0, 0);

    Bl_new[1][i].Duration=num;
    str_num = "";
    //for (int i=0; i<=19; i++) {
    // Bl[1][i].ShowProperties();
    //}
    // Add new line at START of table
    Bl = myConcat (Bl_new, Bl );
  }
}

Blocks[][] myConcat( Blocks[][] newBl, 
  Blocks[][] oldBl ) {

  // Add new line at START of table

  // println(oldBl.length); 
  // make new blank table
  Blocks[][] Bl_result = new Blocks[oldBl.length][oldBl[0].length];

  // init 1st line
  for (int i_row = 0; i_row < 6; i_row++) {
    Bl_result[i_row][0]=new Blocks(0, 0, 0, 0, 0);
    Bl_result[i_row][0]=newBl[i_row][0];
  }

  // copy other lines
  for (int i_row=1; i_row<Bl_result[0].length; i_row++) {
    for (int i = 0; i < 6; i++) {
      Bl_result[i][i_row]=new Blocks(0, 0, 0, 0, 0);
      Bl_result[i][i_row]=oldBl[i][i_row-1];
    }
  }

  // return new table
  return Bl_result;
}

String blinkingCursor() {

  if ((frameCount%11) == 0) {
    showCursorAsLine= ! showCursorAsLine;
  }

  if (showCursorAsLine) {
    return("|");
  } else
  {
    return(" ");
  }
} // method

// =====================================================================================

class Blocks { 

  int Lower, Duration, Start, AVG_RPM, State;

  // constr
  Blocks (int LowerTemp, int DurationTemp, 
    int StartTemp, int AVG_RPMTemp, int StateTemp) {
    Lower=LowerTemp; 
    Duration=DurationTemp;
    Start=StartTemp; 
    AVG_RPM=AVG_RPMTemp;
    State=StateTemp;
  }// constr

  void ShowProperties() { 
    println ("Bl[1]: " +Lower, Duration, Start, AVG_RPM, State);
  }//method

  String getProperties() { 
    return
      Lower +", " 
      + Duration + ", "
      + Start + ", "
      + AVG_RPM + ", "
      + State;
  }//method
  //
}//class 
//

```

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 22, 2022, 7:54pm UTC](https://discourse.processing.org/t/writing-my-first-class-and-getting-in-trouble-already/38844/12 "2022-09-22T19:54:23Z")

</div>

Thanks a lot, that worked
