# TableRow returning NaN values

**URL:** <https://discourse.processing.org/t/tablerow-returning-nan-values/7955>\
**Category:** Coding Questions\
**Created:** [January 30, 2019, 2:50am UTC](https://discourse.processing.org/t/tablerow-returning-nan-values/7955 "2019-01-30T02:50:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![morphogencc](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@morphogencc](https://discourse.processing.org/u/morphogencc)\
**Post date:** [January 30, 2019, 2:50am UTC](https://discourse.processing.org/t/tablerow-returning-nan-values/7955/1 "2019-01-30T02:50:32Z")

</div>

Hello – I’m working on a project that uses CSV data to drive the graphics; I created a Show Control class that reads all of the csv files in a folder, and then grabs the next available row from the current file to provide values for the graphics. However, I’m running into a strange error where every /other/ row returns NaN for the cell values, and then the other rows return valid values.

The class is here:

```auto
class ShowControl {
  ArrayList<Table> mShowList = new ArrayList<Table>(10);
  int mShowIndex = 0;
  int mCurrentShowLocation = 0;
  Table mCurrentShow = null;
  
  ShowControl() {
    String[] fileList;
    File file = new File(dataPath(""));
    if (file.isDirectory()) {
      fileList = file.list();
    } else {
      fileList = null;
    }
    
    printArray(fileList);
    
    if(fileList != null) {
      for(String filename : fileList) {
        Table t = loadTable(filename, "header");
        mShowList.add(t);
      }
    }
    
    if(mShowList.size() > 0) {
      mCurrentShow = mShowList.get(mShowIndex);
    }
  }
  
  void update() {
    if(mCurrentShowLocation < mCurrentShow.getRowCount() - 1) {
      mCurrentShowLocation++;
    }
    else {
      mShowIndex++;
      mShowIndex = mShowIndex % mShowList.size();
      println("Current Show Index: " + mShowIndex);
      mCurrentShow = mShowList.get(mShowIndex);
      mCurrentShowLocation = 0;
    }
  }
 
  void restartShow() {
    mCurrentShowLocation = 0;
  }
  
  float getValue(String fieldName) {
    println("Show Location: " + mCurrentShowLocation);
    println("Field Name: " + fieldName);
    println(mCurrentShow.getFloat(mCurrentShowLocation, fieldName));
    return mCurrentShow.getFloat(mCurrentShowLocation, fieldName);
  }
  
}

```

and the code that I’m calling in draw() is this:

```auto
  mShowControl.update();
  float pos_x = mShowControl.getValue("Gallery X");
  float pos_y = mShowControl.getValue("Gallery Y");
  float pos_z = mShowControl.getValue("Gallery Z");

```

which prints data like this:

```auto
Show Location: 155
Field Name: Gallery X
69.0
Show Location: 155
Field Name: Gallery Y
69.0
Show Location: 155
Field Name: Gallery Z
2.37
Show Location: 156
Field Name: Gallery X
NaN
Show Location: 156
Field Name: Gallery Y
NaN
Show Location: 156
Field Name: Gallery Z
NaN

```

The csv file looks fine in every way, so I’m very confused how even rows provide incorrect data but odd rows work fine!

Does anyone have any thoughts?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 30, 2019, 5:12am UTC](https://discourse.processing.org/t/tablerow-returning-nan-values/7955/2 "2019-01-30T05:12:28Z")

</div>

first i did not get into your code,  
but want start differently:

sadly

- array
- arraylist
- table

have very different style ( and functionalities )  
but what i not understand is your idea of ?copy?  
a table to a arraylist  
example see

> **[TableRow / Reference](https://processing.org/reference/TableRow.html)**
>
> A TableRow object represents a single row of data values, stored in columns, from a Table. Additional TableRow methods are documented in the

so as you might want to build a screen with the content of a table row  
( one by one by like [NEXT] button… ?)  
you just load a record ( line ) ONE\_ROW  
and ( that part i also not see ) show its columns/fields ? by

> text(ONE\_ROW.getString(“thatColumn”),posx,posy);

but if you want someone here to dig deeper into your code  
post a running minimal code example  
and also a table\_file.csv example;

a easy way:

```auto
// https://discourse.processing.org/t/tablerow-returning-nan-values/7955

Table mytable;
TableRow one_row;
String scol, myfile = "data/table.csv"; // with column headers
int xpos, ypos, dx, line=0, crow, ccol;

void setup() {
  size(200, 200);  
  mytable = loadTable(myfile, "header");
  crow = mytable.getRowCount();  
  ccol = mytable.getColumnCount();
  println("file: "+myfile+" rows: "+crow+" cols: "+ccol);
  println("use: key [n] for next");
}

void draw() {
  background(200, 200, 0);
  draw_row(line);
}

void draw_row(int line) {
  one_row = mytable.getRow(line);
  scol = "Gallery ";
  xpos = 10; ypos =20; dx = 60;
  fill(0,0,200);
  text(scol+'X',xpos,ypos); xpos += dx;
  text(scol+'Y',xpos,ypos); xpos += dx;
  text(scol+'Z',xpos,ypos); 
  xpos = 10; ypos += ypos;
  text(nf(one_row.getFloat(scol+'X'),1,1),xpos,ypos); xpos += dx;
  text(nf(one_row.getFloat(scol+'Y'),1,1),xpos,ypos); xpos += dx;
  text(nf(one_row.getFloat(scol+'Z'),1,1),xpos,ypos); 
}

void keyPressed() {
  if (key == 'n') line++;
  if (line >= crow ) line =0;
}

/*
data/table.csv content:

Gallery X,Gallery Y,Gallery Z,
11.0,12.0,13.0,
21.0,22.0,23.0,

*/

```

 ![2019-01-30_13-00-35_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e8d2ea9bdaa51f7b610b074d4ed9521cae75e97a.png)

//_ **UPDATE** _\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
i did try your code ( with what i needed to test it ) and it worked.

```auto
ShowControl mShowControl;

class ShowControl {
  ArrayList<Table> mShowList = new ArrayList<Table>(10);
  int mShowIndex = 0;
  int mCurrentShowLocation = 0;
  Table mCurrentShow = null;

  ShowControl() {
    String[] fileList;
    File file = new File(dataPath(""));
    if (file.isDirectory()) {
      fileList = file.list();
    } else {
      fileList = null;
    }

    printArray(fileList);

    if (fileList != null) {
      for (String filename : fileList) {
        Table t = loadTable(filename, "header");
        mShowList.add(t);
      }
    }

    if (mShowList.size() > 0) {
      mCurrentShow = mShowList.get(mShowIndex);
    }
  }

  void update() {
    if (mCurrentShowLocation < mCurrentShow.getRowCount() - 1) {
      mCurrentShowLocation++;
    } else {
      mShowIndex++;
      mShowIndex = mShowIndex % mShowList.size();
      println("Current Show Index: " + mShowIndex);
      mCurrentShow = mShowList.get(mShowIndex);
      mCurrentShowLocation = 0;
    }
  }

  void restartShow() {
    mCurrentShowLocation = 0;
  }

  float getValue(String fieldName) {
    println("loc: " + mCurrentShowLocation+" field: " + fieldName+" val:"+mCurrentShow.getFloat(mCurrentShowLocation, fieldName));
    return mCurrentShow.getFloat(mCurrentShowLocation, fieldName);
  }
}

void setup() {
  size(200,200);
  mShowControl = new ShowControl();
}

void draw() {
 background(200,200,0);
  mShowControl.update();
  float pos_x = mShowControl.getValue("Gallery X");
  float pos_y = mShowControl.getValue("Gallery Y");
  float pos_z = mShowControl.getValue("Gallery Z");
}

/*
data/table.csv

Gallery X,Gallery Y,Gallery Z,
11.0,12.0,13.0,
21.0,22.0,23.0,

data/table2.csv

Gallery X,Gallery Y,Gallery Z,
211.0,212.0,213.0,
221.0,222.0,223.0,

*/
// show
/*
Current Show Index: 0
loc: 0 field: Gallery X val:11.0
loc: 0 field: Gallery Y val:12.0
loc: 0 field: Gallery Z val:13.0
loc: 1 field: Gallery X val:21.0
loc: 1 field: Gallery Y val:22.0
loc: 1 field: Gallery Z val:23.0
Current Show Index: 1
loc: 0 field: Gallery X val:211.0
loc: 0 field: Gallery Y val:212.0
loc: 0 field: Gallery Z val:213.0
loc: 1 field: Gallery X val:221.0
loc: 1 field: Gallery Y val:222.0
loc: 1 field: Gallery Z val:223.0
*/

```

so ?? one of your test files is bad??

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [January 30, 2019, 5:12am UTC](https://discourse.processing.org/t/tablerow-returning-nan-values/7955/3 "2019-01-30T05:12:47Z")

</div>

Do you experience the same problem if you use another csv file? Can you share a small portion of the file here to see the structure? It will also help to reproduce the problem.

Kf

---

<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:** [January 30, 2019, 10:25am UTC](https://discourse.processing.org/t/tablerow-returning-nan-values/7955/4 "2019-01-30T10:25:31Z")

</div>

Some useful Table methods: 🕶

1. **setColumnTitles()**: [Forum.Processing.org/two/discussions/tagged?Tag=setcolumntypes()](http://Forum.Processing.org/two/discussions/tagged?Tag=setcolumntypes%28%29)

2. **setMissingFloat()**: [Forum.Processing.org/two/discussion/17738/test-a-variable-type](http://Forum.Processing.org/two/discussion/17738/test-a-variable-type)

3. Table docs: [Processing.GitHub.io/processing-javadocs/core/processing/data/Table.html](http://Processing.GitHub.io/processing-javadocs/core/processing/data/Table.html)

---

<div class="post-metadata">

**Author:** ![morphogencc](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@morphogencc](https://discourse.processing.org/u/morphogencc)\
**Post date:** [January 30, 2019, 3:39pm UTC](https://discourse.processing.org/t/tablerow-returning-nan-values/7955/5 "2019-01-30T15:39:12Z")

</div>

Thanks for your help everyone – I went in to copy/paste a subsection of my CSV file, and realized that the software I was using to generate it was adding extra carriage returns that weren’t showing up in my text editor. This resulted in the every-other-line being null data, and now that I fixed it, everything is working!
