I'm trying to read from a json file in order to create visual images via processing. My code so far is:
public void generateShape(JSONObject file) {
//convert file to json object
JSONArray shapes = (JSONArray) file.get("shapes");
for (Object o : shapes) {
JSONObject json_shape = (JSONObject)o;
//getting shape id and function so we know which shapes to create
id = ((Number) json_shape.get("id")).intValue();
func = ((Number) json_shape.get("func")).intValue();
animate = ((Number) json_shape.get("animate")).intValue();
stroke_weight = ((Number) json_shape.get("stroke_weight")).floatValue();
JSONArray strokes = (JSONArray) json_shape.get("stroke");
Iterator<Float> iterator = strokes.iterator();
index = 0;
while(iterator.hasNext())
{
stroke[index] = ((Number)iterator.next()).floatValue();
index++;
}
stoke[] is an array of floats which will control the colour of the stroke. The json file im parsing from is
{
"shapes": [
{
"id":0,
"func":1,
"stroke":[143,50,200],
"stroke_weight": 70,
"animate": 1,
"jelly_func": 5,
"points":[268,330,400,333,74,210,90,300]
}
]
}
im getting the error at the line:
stroke[index] = ((Number)iterator.next()).floatValue();
I'm using the json.simple library and all solutions i've seen are for org.json library
I could hard code the values in the stroke array but I wanted to try iteration instead. Any tips/guidance?
full stack trace is
java.lang.NullPointerException
at ImageLoader.generateShape(ImageLoader.java:35)
at Sketch.setup(Sketch.java:48)
at processing.core.PApplet.handleDraw(PApplet.java:2404)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
where sketch is:
ImageLoader image = new ImageLoader();
JSONParser parser = new JSONParser();
try {
JSONObject file = (JSONObject) parser.parse((new FileReader("save_data.json")));
image.generateShape(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
eta: ok so upon removing the line causing the exception, and checking if my json values are even being read I can confirm the values being parsed are correct when I print them to console. I just now need to put them in an array