1

I am writing a java program fetching JSON data through HTTP GET methods and it returns the following after I navigate the object tree:

{
"year":"2015",
"period":"M03",
"periodName":"March",
"value":"141178",
"footnotes":[{}]
}

Now I want to take value and caste it to a float, I tried to do this like such:

JSONParser parser = new JSONParser();

try
{
    JSONObject
    BLSemployment = ( JSONObject ) parser.parse( _RDATA );
    BLSemployment =       ( ( JSONObject ) BLSemployment.get( "Results" ) );

    JSONArray
    BLSemploymentseries = ( ( JSONArray ) BLSemployment.get( "series" ) );

    BLSemployment =       ( ( JSONObject ) BLSemploymentseries.get( 0 ) );
    BLSemploymentseries = ( ( JSONArray ) BLSemployment.get( "data" ) );

    for( int i = 0; i < 12; i++ )
    {
        BLSemployment = ( (JSONObject) BLSemploymentseries.get( i ) );

        HistoricalNonFarmPayrollData[i] = Float.parseFloat( JSONValue.toJSONString( BLSemployment.get( "value" ) ).replace( "\"" , "" ) );
        HistoricalNonFarmPayrollYear[i] = JSONValue.toJSONString( BLSemployment.get( "year" ) );
        HistoricalNonFarmPayrollMonth[i] = JSONValue.toJSONString( BLSemployment.get( "periodName" ) );
    }
}
catch ( ParseException pe )
{
    System.out.println( pe );
}

However Now I get the error:

Exception in thread "main" java.lang.NullPointerException
        at BLSFramework.getNonFarmPayrolls(playground.java:354)
        at playground.main(playground.java:27)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
FX_NINJA
  • 103
  • 5
  • Split up the line into several lines and see what `null` is or use a debugger! – MalaKa Jul 02 '15 at 20:18
  • I don't understand this part HistoricalNonFarmPayrollData[i] = Float.parseFloat( JSONValue.toJSONString( BLSemployment.get( "value" ) ).replace( "\"" , "" ) ); BLSemployment.get( "value" ) seems to return already string. Shouldn't be ? HistoricalNonFarmPayrollData[i] = Float.parseFloat( BLSemployment.get( "value" ) ); – Robert Wadowski Jul 02 '15 at 20:21
  • you did not put the code that throws the exception. You should post that also. – iullianr Jul 02 '15 at 20:21
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/1768232) – durron597 Jul 02 '15 at 20:28

1 Answers1

0

RESOLVED: occasionally BLS website doesn't send data for periods of time and I was coding during that period of time.

FX_NINJA
  • 103
  • 5