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