We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello! im trying to write a kind of user interface to manage JSON files. What i want is user can store data in JSON files. when mousePressed save a new JSON FILE. here`s my code with a null pointer exception:
JSONObject json;
File saving;
File loading;
void setup() {
json = new JSONObject();
}
void draw(){
}
void mousePressed() {
selectInput("Select a file to process:", "fileSelected");
saveJSONObject(json, saving.getAbsolutePath());
}
void fileSelected(File selection) {
saving = selection;
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
}
}
Answers
Which line Shows error?
Ah 16
In draw loop until saving is != null
Only then we can save
The command fileSelected doesn’t stop/ pause for the user hence 16 is reached immediately without saving is set.
It’s a little confusing. But it’s a 2nd thread that does the file dialogue while the sketch runs on and on
it works, but i need to click twice (one for choose the place, and one for save the file) why that?
This belongs into draw()
if(saving != null)
saveJSONObject(json, saving.getAbsolutePath());
Hmmm actually it belongs to the callback function if
mousePressed()
is the trigger to save the data:Kf
Good idea
There is also a new forum
full new version with load and save:
see comments below:
Chrisir ;-)
If we got the keys & values as separate arrays, we can create a StringDict passing both arrays as its arguments; and then create our JSONObject passing that StringDict as its argument. Much shorter & cleaner! \m/
Nice!
thanks guys. im understanding a little bit better all of json world. now my problem is how to store the type of an Object for later restore this data. I`ve a system with an array. using polymorphism two Obj(sphere, can) extends main Obj. How can indicate what type of object are in JSON to later restore?
Just invent a further field like xPos , name it typeOfObject and store "can" or "sphere" in it