0

I am parsing a JSONObject which I extract from a Mongo DB. The number values map to java.lang.number objects according to the documentation, However, I get an error when I try to make the following declarations, specifically in the first System.out.println(value.floatVlaue()).

    //For data
    int counter = 0;
    float score = 0;
    Number value = 0;

    while(cur.hasNext()){
        JSONObject jsonObj = (JSONObject) JSONValue.parse(cur.next().toString());
        metricsIterator = ((JSONObject)((JSONObject)((JSONObject)((JSONObject)jsonObj.get("level_1")).get("level_2")).get("level_3")).get("metrics")).keySet().iterator();

        while(metricsIterator.hasNext()){
            System.out.println(metricsIterator.next().toString());
            value = (Number) jsonObj.get(metricsIterator.next());

            System.out.println(value.floatValue());

            System.out.println(value.toString());

            someLogic(value.doubleValue(), ((Number)jsonObj.get("long")).intValue(), start, end, days);
        }
    }

I think the problem line definitely comes from the value = (Number) jsonObj.get(metrics Iterator.next())

the Json im parsing looks like this

    { "_id" : "9223370663181869423_cnn_1" , 
    "long" : 1373672925000 , 
    "level_1" : 
            { "level_2" : 
                { "level_3level" : 
                    { "metrics" : 
                          {"key1" : 0.003333333333333334 , 
                          "key2" : 0.005833333333333334 , 
                          "key3" : 0.005833333333333334 , 
                          "key4" : 0.009166666666666667 , 
                          "key5" : 0.1391666666666667 , 
                          "key6" : 0.1241666666666667 , 
                          "key7" : 0.01666666666666667
                           }
                     }
                 }
            }
    }

Thanks so much for any help at all!

2 Answers 2

1

Iterators can only be traversed once. This means that once you do metricsIterator.next() in System.out.println(metricsIterator.next().toString());, you end up using up that value and when you call .next() again in value = (Number) jsonObj.get(metricsIterator.next()); you get the value after the one that you are expecting, in other words you skip a value. As a result when you hit the last value, the next value is null which results in the null pointer. Try removing the sysout before assignment to value, it should solve the nullpointer.

Sign up to request clarification or add additional context in comments.

Comments

0

you are doing twice the metricsIterator.next()

first: System.out.println(metricsIterator.next().toString()); second: value = (Number) jsonObj.get(metricsIterator.next());

then you get to the end without checking if hasNext()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.