We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I made this in Intelij, with processing, trying to write a csv. But this code gives me an error 'Exception in thread "Animation Thread" java.lang.NullPointerException' when I write the csv with saveTable, why?
When I print out the table I get 'processing.data.Table@66ede931', giving me proof that the table is really there!, so the bug must be the in the path?
import processing.core.PApplet;
import processing.data.Table;
import processing.data.TableRow;
public class WriteCsv {
static PApplet parent;
public WriteCsv(PApplet p) {
parent = p;
}
Table table;
public void createNewCsv() {
table.addColumn("M_Phase");//342
table.setInt(1, "M_Phase", 0);//342
parent.println("table : " + table ); // prints out 'processing.data.Table@66ede931', so table is not a null!!
parent.saveTable( table, "data/info.csv" );
}
}
Any ideas what can cause the this error?
Answers
Got no InteliJ but made a PDE sketch example using a separate ".java" tab.
Which is hopefully pretty close to other IDEs' behavior. :\">
"Save_Load_Table.pde":
"WriteCSV.java":
Wow, that totally worked! I was really surprised that the solution is so much different compared with the example at the processing site! Its a lot more code to chew trough and to understand, but it perfectly works! Many many thanx You really made my day!!
All you were missing was a call to initiate Table.
Your line 13 just defines the table to be of type Table, but it doesn't instantiate it.
@GoToLoop How would you modify your code so to append to a file? Is it possible to use your sample or would I need to write appending operations using explicit java code aka. writing my own code?
On another note, I notice the following code which I do not recognize:
You are returning your own class where the method was defined. I can see some functionality in this structure (I have seen it when using ControlP5 objects). Is this something that you find in java coding (I haven't seen it in C/C++). Would that fall under OOP design?
Lastly, in your code line 41 when using the StringBuilder object, don'y you need to define the column ID when implementing the object, beside the column name? When you do the for loop in line 43/44, you are building "rowNumber:columnValue" pairs.
Kf
It's completely relying on Processing functions: loadTable() & saveTable().
Just call loadCSVTable() method, do your appending stuff w/ addCSVRow() or something else, then call saveCSVTable().
Yea! More precisely, it's returning the same current instance. That's what
this
keyword means.Well, it's completely language agnostic. Any OOP language can return
this
orself
in order to provide method chaining. :ar!Dunno. It's just a fun pattern. Any method which would be
void
can instead returnthis
. :DBtW, Processing's PVector class got fully chainable at version 3.
Same for Processing's JS respin: p5.js. :P
I don't get what you mean. Class StringBuilder belongs to Java's API and got nothing to do w/ Processing: http://docs.Oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Method toString() is just for displaying purposes. This particular example only got 1 column. So it's always index 0 for it. Then I build the StringBuilder row by row. :-B
"Fully chainable" is interesting concept indeed. Thxs!
Kf
Of course, the once non-
void
methods aren't chainable. :-@Although any other returning object can start another type of chain. :P