Ahmed Garssallaoui
Ahmed Garssallaoui

Reputation: 167

java.lang.NullPointerException JSONObject Array in JAVA

I just starting working with the java JSON library (org.json.simple.JSONObject). In my code, I needed to make a JSONARRAY of JSONOBJECTS, however instead of finding the 3 different objects in my array, I find the the last object duplicated.

The function code:

private void setMesurement(Date[] ts,  Integer[][] time, String mesurementLabel, Object[][] mesurementValue, Integer[][] unit ) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.FRENCH);
        JSONArray measurements = new JSONArray();
        JSONObject series = new JSONObject();
        JSONArray $_time = new JSONArray();
        JSONArray msrVal = new JSONArray();
        JSONArray unitVal = new JSONArray();

        JSONObject msr = new JSONObject();

        for (int i=0; i<ts.length; i++) {
            
            $_time.clear();
            msrVal.clear();
            unitVal.clear();
            
           for (int j=0; j<time[i].length; j++) {               
                $_time.add(time[i][j]);
                msrVal.add(mesurementValue[i][j]);
                unitVal.add(unit[i][j]);
           }

            series.put("$_time", $_time);
            series.put(mesurementLabel, msrVal);
            series.put("unit", unitVal);            
            msr.put("series", series);
            msr.put("ts", dateFormat.format(ts[i]));
            measurements.add(msr);

        }

        this.ppmp.put("measurements", measurements);
        System.out.println("adding measurements: "+measurements+" to ppmp");
    }

and this is the duplicated result:

  "measurements":[
    {"series":{
      "heat":[7,8,9],
      "unit":[2,2,2],
      "$_time":[0,16,36]},
     "ts":"2018-04-23T11:30:35.587+0100"},
    
    {"series":{
      "heat":[7,8,9],
      "unit":[2,2,2],
      "$_time":[0,16,36]},
     "ts":"2018-04-23T11:30:35.587+0100"},
    
    {"series":{
      "heat":[7,8,9],
      "unit":[2,2,2],
      "$_time":[0,16,36]},
     "ts":"2018-04-23T11:30:35.587+0100"}
  ]

Upvotes: 0

Views: 605

Answers (1)

Malte Hartwig
Malte Hartwig

Reputation: 4555

You are not creating new JSON Objects for each iteration. You only create one at the beginning, set its values, and add it to the list. Then you overwrite the same object's values, and add it to the list again. In the end, you have added the object to the list three times, and its values will be the ones from the last iteration.

To fix this, change

JSONObject series = new JSONObject();
JSONArray $_time = new JSONArray();
JSONArray msrVal = new JSONArray();
JSONArray unitVal = new JSONArray();
JSONObject msr = new JSONObject();

for (int i=0; i<ts.length; i++) {
    $_time.clear();
    msrVal.clear();
    unitVal.clear();
    ...
    measurements.add(msr);
}

to

for (int i=0; i<ts.length; i++) {
    JSONObject series = new JSONObject();
    JSONArray $_time = new JSONArray();
    JSONArray msrVal = new JSONArray();
    JSONArray unitVal = new JSONArray();
    JSONObject msr = new JSONObject();
    ...
    measurements.add(msr);
}

This way, you create a new object for every step and avoid changing the previous ones.

Upvotes: 1

Related Questions