Help with data.csv code

Hello I am trying to run the code which is from processing.org and using data.csv file, but I am getting the following error:
This tabel has no column named x.
The code is as following

Table table;
Bubble[] bubbles;

void setup() {
  size(480, 360);
  loadData();
}

void draw() {
  background(255);
  // Display all bubbles
  for (int i = 0; i<bubbles.length; i++) {
    bubbles[i].display();
  }
}

void loadData() {
  // "header" indicates the file has header row. The size of the array 
  // is then determined by the number of rows in the table. 
  table = loadTable("data.csv", "header");
  bubbles = new Bubble[table.getRowCount()];


  for (int i = 0; i<table.getRowCount(); i++) {
    // Iterate over all the rows in a table.
    TableRow row = table.getRow(i);

  
    // Access the fields via their column name (or index).
    float x = row.getFloat("x");
    float y = row.getFloat("id");
    float d = row.getFloat("diameter");
    String n = row.getString("name");
    // Make a Bubble object out of the data from each row.
    bubbles[i] = new Bubble(x, y, d, n);
  }
}


void mousePressed() {
  // When the mouse is pressed, create a new row and set the values for each column of that row.
  TableRow row = table.addRow();
  row.setFloat("x", mouseX);
  row.setFloat("y", mouseY);
  row.setFloat("diameter", random(40, 80));
  row.setString("name", "Blah");

  // If the table has more than 10 rows, delete the oldest row.
  if (table.getRowCount()>10) {
    table.removeRow(0);
  }

  // This writes the table back to the original CSV file
  // and reloads the file so that what's drawn matches.
  saveTable(table, "data/data.csv");
  loadData();
}

// This simple Bubble class draws a circle to the window 
// and displays a text label when the mouse hovers.
class Bubble {
  float x, y;
  float diameter;
  String name;
  
  boolean over = false;
  
  // Create the Bubble
  Bubble(float tempX, float tempY, float tempD, String s) {
    x = tempX;
    y = tempY;
    diameter = tempD;
    name = s;
  }
  
  // Checking if mouse is over the bubble
  void rollover(float px, float py) {
    float d = dist(px, py, x, y);
    if (d<diameter/2) {
      over = true; 
    } else {
      over = false;
    }
  }
  
  // Display the Bubble
  void display() {
    stroke(0);
    strokeWeight(2);
    noFill();
    ellipse(x, y, diameter, diameter);
    if (over) {
      fill(0);
      textAlign(CENTER);
      text(name, x, y+diameter/2+20);
    }
  }
}
1 Like

please format your code first
and again, if the error is about the file content,
why you not show the file content,
and as csv file is text you also post it using the

</> code tag

```
your code or data
```

1 Like

Based on your code, mousePressed is expecting the first line of your CSV file to be a header line, and to contain (in some order):

x, y, diameter, name

So if instead of “x” your first line contains “X” or “x-value” – or anything else – setting the column “x” will fail, because it doesn’t exist.

Hello
My csv file is as following
image
But still gettting same error
Any help will be appreciated
Thanks

this is ? a excel/spread sheet ? view,
show the text of the data.csv file
i want see the ","s

add it should be in sub dir /data/
and pls call it with

String infile = "data/data.csv"
Table table;

void setup() {
  table = loadTable(infile,"header , csv" );
}

İt is a csv file
I saved it as text file as following

x y diameter name
160 103 4.319.838 Happy
372 137 5.242.526 Sad
273 235 6.114.072 Joyous
121 179 44.758.068 Melancholy

if it looks like that it is NOT a csv file.
if the separator is " " space it will never work in processing,
if it is separated by [tab] it should be named data.tsv
and called with

String infile = "data/data.tsv"
Table table;

void setup() {
  table = loadTable(infile,"header , tsv" );
}

but if you use csv you must use “,”

and when you post data you format it with the

</> code tag

not with the " quote tag


how is it possible that you have such a long code with class and …
without being able to read a file, are you working backward or using
other code?

if you start with table and file load your code should be


Table table;
int i, k, trows, tcols;

void setup() {

  table = loadTable("test.tsv", "tsv");  // "test.csv" // "test.tsv" , "tsv" // "test.tsv" , "header , tsv"
  trows=table.getRowCount();
  tcols=table.getColumnCount();

  println(trows + " rows/lines in table "); //(? - header ?)
  println(tcols + " cols in table");
  println("header: ");
  for ( i =0; i < tcols; i++) {
    println("col: "+i+" "+ table.getColumnTitle(i));
  }

  for ( k =0; k < trows; k++) {
    for ( i =0; i < tcols; i++) {
      println("row: "+k+" col: "+i+" string: "+table.getString(k, i));
    }
  }
}


I modified my csv fila but now I am getting NullPointerException errror
My code is:

String infile = "data/data.csv";
Table table;
Bubble[] bubbles;



void setup() {
  table = loadTable(infile,"header , csv" );
}
void draw() {
  background(255);
  // Display all bubbles
  for (int i = 0; i<bubbles.length; i++) {
    bubbles[i].display();
  }
}

void loadData() {
  // "header" indicates the file has header row. The size of the array 
  // is then determined by the number of rows in the table. 
  //table = loadTable(infile, "header , csv");
  bubbles = new Bubble[table.getRowCount()];


  for (int i = 0; i<table.getRowCount(); i++) {
    // Iterate over all the rows in a table.
    TableRow row = table.getRow(i);

  
    // Access the fields via their column name (or index).
    float x = row.getFloat("x");
    float y = row.getFloat("id");
    float d = row.getFloat("diameter");
    String n = row.getString("name");
    // Make a Bubble object out of the data from each row.
    bubbles[i] = new Bubble(x, y, d, n);
  }
}


void mousePressed() {
  // When the mouse is pressed, create a new row and set the values for each column of that row.
  TableRow row = table.addRow();
  row.setFloat("x", mouseX);
  row.setFloat("y", mouseY);
  row.setFloat("diameter", random(40, 80));
  row.setString("name", "Blah");

  // If the table has more than 10 rows, delete the oldest row.
  if (table.getRowCount()>10) {
    table.removeRow(0);
  }

  // This writes the table back to the original CSV file
  // and reloads the file so that what's drawn matches.
  saveTable(table, "data/data.csv");
  loadData();
}

// This simple Bubble class draws a circle to the window 
// and displays a text label when the mouse hovers.
class Bubble {
  float x, y;
  float diameter;
  String name;
  
  boolean over = false;
  
  // Create the Bubble
  Bubble(float tempX, float tempY, float tempD, String s) {
    x = tempX;
    y = tempY;
    diameter = tempD;
    name = s;
  }
  
  // Checking if mouse is over the bubble
  void rollover(float px, float py) {
    float d = dist(px, py, x, y);
    if (d<diameter/2) {
      over = true; 
    } else {
      over = false;
    }
  }
  
  // Display the Bubble
  void display() {
    stroke(0);
    strokeWeight(2);
    noFill();
    ellipse(x, y, diameter, diameter);
    if (over) {
      fill(0);
      textAlign(CENTER);
      text(name, x, y+diameter/2+20);
    }
  }
}

format your code
and post your data file content also formatted

and see my above example how to start with table,
load / read / print

insofar i am not interested in the rest of your code
as long it is not clear what you load and how it is understood.

Thanks alot, you wrote the code for me; I noticed it now, appreciate your help.

@Seyfi – a key thing to learn here is that it is important to share exactly what your input data is so that others can help you test and debug a data problem. Not data loaded in an app. Not a screenshot. Not an HTML view or markdown formatted view. Just the actual data. That is what the program needs, and what causes errors, so that is what you should share.

If you have reason that you need to use tabs instead of commas, loadTable has a tsv mode. However using commas is often simpler.

When you say “I modified my csv, but now”… again, we can’t possibly inspect or test, because again you haven’t shared your actual data (or at least a sample of it).

1 Like

your code show a change with setup load the table,

but not init the bubbles anymore
and not load the data from table to class array any more ?

but in draw you try to draw the ( uninitialized ) bubbles what sure gives a error.