A quirky problem with string arrays and Null Pointer Exception

thank you for your kind words.

here is a version where I use a real Table to show the vocabulary

It’s just where you click solution, I think when you click it in my posts, this posts will be the solution.

Warmest Regards,

Chrisir


/**
 * adapted from the Processing example - LoadFile 1
 * 
 * Loads a text file that contains two strings separated by a tab ('\t').
 * and then....
 */
String[] words;
String[] french;
String[] english;
int iRun, iWord;

// class with tools 
Tools tools = new Tools(); 

// the table
Table tResources; 


void setup() {

  size(1200, 500);
  background(0);
  stroke(255);
  frameRate(12);

  words = loadStrings("vocab.txt");
  println("there are " + words.length + " word pairs");
  for (int i = 0; i < words.length; i++) {
    println(words[i]);
  }

  french = new String[words.length+1];
  english = new String[words.length+1]; 

  french[0]="French";
  english[0]="English";

  for (int i = 0; i < words.length; i = i+1) {
    String[] frenchEnglish = split(words[i], ' ');
    println(frenchEnglish[0]);
    int iWord=int(frenchEnglish[0]);
    println("French word ", iWord, " is ", frenchEnglish[1]);
    println("English word ", iWord, " is ", frenchEnglish[2]);
    int iPair=i+1;
    french[iPair]= frenchEnglish[1];
    english[iPair]= frenchEnglish[2];
    println("iPair = ", iPair, "    French = ", french[iPair], "    English =", english[iPair]);
  }
  for (int i=0; i < words.length+1; i++) {
    println(i, " ", french[i], " ", english[i]);
  }
  iRun=1;
  iWord=1;

  setupTable() ;

  println("leaving setup ----------------------------------");
}

void draw() {
  background(0);

  tools.showTable(tResources, 
    22, 22);
}

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

void setupTable() {

  // setting up table (column names / headlines)
  tResources = tools.newTable("No", "French", "English");
  // adding data (must match headlines)
  for (int i=0; i < words.length+1; i++) {
    tResources = tools.tableAddData ( tResources, str(i), french[i], english[i] );
  }
}

//===============================================================================
// Tools collection 

class Tools {

  // class not like a car class Car for an object but a collection of tools. 

  final color RED   = color(255, 0, 0); 
  final color GREEN = color(0, 255, 0); 
  final color BLUE  = color(0, 0, 255); 

  final color WHITE = color(255); 
  final color BLACK = color(0); 
  final color GRAY  = color(255/2); 

  boolean cursorSignShowFlag=false; 

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

  void showTable(Table tableBtn, 
    int x, int y) { 

    int factorX=78; // column width 

    // rect
    stroke(WHITE);
    noFill();
    rect( x, y, 
      tableBtn.getColumnCount()*78-6, (tableBtn.getRowCount()+1) * 22 + 10 );

    // headline 
    showTableHeadline(tableBtn, x+6, y+19, factorX);

    // horizontal line 
    stroke(WHITE);
    line( x+2, y+5+19, 
      6+x+(tableBtn.getColumnCount())*factorX-13, y+5+19);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x) 
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {

        fill(WHITE);
        text(row.getString(i2), 
          i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15);

        if (mouseInside(i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15)) {
          text (row.getString(i2), 
            20, height-22);
        }//if

        // vertical line 
        line( i2*factorX+x, +y, 
          i2*factorX+x, tableBtn.getRowCount() * 22 + y + 31);
      }//for
    }//for
  }// method 

  boolean mouseInside( float x_, float y_, 
    float w_, float h_) {
    return mouseX>x_ &&
      mouseX<x_+w_ &&
      mouseY>y_ &&
      mouseY<y_+h_;
  }

  void showTableHeadline(Table tableBtn, 
    int x, int y, 
    int factorX) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      fill(GREEN);
      text(row0.getColumnTitle(i), 
        i*factorX+x, y-2);
    }
  }//method 

  // ---
  // make table 

  Table newTable (String... listColumnNames) { 

    Table newT = new Table();

    // make columns
    for (String s1 : listColumnNames) {
      newT.addColumn(s1);
    }

    return newT;
  }//method

  Table tableAddData(  Table table1, String... data1  ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (String s1 : data1) {
      newRow.setString(newRow.getColumnTitle(i), s1);
      i++;
    }


    return table1;
  }//method

  // --- 

  void printlnTable(Table tableBtn) { 

    // rect
    stroke(WHITE);
    noFill();

    println("------------------------------------");

    // headline 
    printlnTableHeadline(tableBtn);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x)
      String s1="";
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {
        s1+="   "+row.getString(i2);
        //
      }//for
      println(s1);
    }//for
    println("===========================================");
    //
  } // method 

  void printlnTableHeadline(Table tableBtn ) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      print("   "+row0.getColumnTitle(i));
    }
    println("");
  }//method 

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

  String cursorSign() {
    // blinking cursor sign | for Input Box 
    if (frameCount % 13 == 0)
      cursorSignShowFlag= ! cursorSignShowFlag;
    if (cursorSignShowFlag)
      return"|";
    else return"";
  }//method

  //
}//class
//

/*
 the following is the "data" file vocab.txt
 
 1  oui  yes
 2  alors  so, then
 3  etre  to be
 4  faire  to do
 5  quoi  what
 
 */
2 Likes