We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everybody, I am pretty new to processing, and just ran into the following problem
I am importing values from a json file, and when I run the following code I get a Nullponter Exception error. I googled and tried different things, but unfortunately I cannot solve the problem. I am greatful for any help, here is my code:
JSONObject json;
int max;
void setup() {
size(841,594);
JSONArray jsonArray = loadJSONArray("january_2016.json");
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONObject maxTemperature = json.getJSONObject("Max Temperature");
int max = maxTemperature.getInt("max");
print(max);
}
void draw(){
//Max Temperature
textAlign(CENTER);
text("Max Temperature",841/4, 594/2+100);
ellipseMode(RADIUS);
ellipse(841/4, 594/2, max, max); // max
ellipse(841/4, 594/2, max, max); //avg
ellipse(841/4, 594/2, max, max); //min
}
Answers
It would help a lot if you had said which statement thrown the NPE. (:|
In case it was method getInt(), we can specify a default value in case of error:
https://Processing.org/reference/JSONObject_getInt_.html
I don`t know what you mean with what statement. I also checked the processing references and still did not find an aswer. Any suggestions what could be the problem in my code?
If you were getting a NPE, you'd know what line it was on.
On line 9 you redefine max to be a local variable. You shouldn't - this hides access to the global variable with the same name.
Change line 9 so it starts:
That was already helpful! But now I am still getting an NPE on line 8
Variable json was globally declared at the top of the sketch:
JSONObject json;
.However it's never got initialized. Therefore json is still
null
when reaching line #8:https://Processing.org/reference/null.html