Writing my first Class, and getting in trouble already

please format code with </> button * homework policy * asking questions

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

Thanks


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

} 

Hello,

Resources:

You need to initialize the array:

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!

:)

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.


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

} 

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

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

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

1 Like

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:


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

1 Like

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

1 Like

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…
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 
//
1 Like

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

1 Like

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 
//

1 Like

Thanks a lot, that worked

1 Like