0

I'm building my first Android app and it requires pulling is JSON formatted data from a local web server. The web server is up and running and I can see the returned data while debugging the Android app. However, while trying to parse the data into a JSON Array, I keep hitting a NullPointerException at the last line of code.

The call:


        JSONParserTask jParser = new JSONParserTask();
        ArrayList> ResultsList = new ArrayList>();
        EditText etSoNo = (EditText) findViewById(R.id.txtSoNo);
        //String url = "http://xxx.xxx.xxx.xxx/api/SOLocLookup/" + etSoNo.getText();
        String url = "http://headers.jsontest.com/";

        try{
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpGet httpget = new HttpGet(url);
            httpget.setHeader("Content-type", "application/json");
            httpget.setHeader("Accept", "JSON");

            InputStream inputStream = null;
            String result = null;

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            result = sb.toString();

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            JSONArray jArray = new JSONArray(result);

At first I thought that maybe my API wasn't sending out a JSON formatted response, so you can see that I changed the call to a test JSON service: http://headers.jsontest.com/

The return data:

{ "Accept-Language": "en-US,en;q=0.8", "Host": "headers.jsontest.com", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8" }

My LogCat:

09-26 18:08:30.977 61-214/system_process I/ActivityManager: Starting: Intent { cmp=com.evs.sodscamera/.activity.SoMatlLookup.SoMatlLookup } from pid 1034 09-26 18:08:31.997 61-99/system_process I/ActivityManager: Displayed com.evs.sodscamera/.activity.SoMatlLookup.SoMatlLookup: +913ms 09-26 18:09:11.748 1034-1034/com.evs.sodscamera D/dalvikvm: JDWP invocation returning with exceptObj=0x40589210 (Ljava/lang/NullPointerException;)

I'm really at a loss. I'm sure that it's something so simple, but with my inexperience I just can't find it.

Thanks!!

-Greg

Update 1:

Changing the JSONArray to a JSONObject works for the data returned from the test website. But when I go to retrieve my original data, it actually is stored as an Array. I should have realized that earlier and not posted using bad test data. My apologies.

New Return Data:

[{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\"","Size1":48.00000,"Size2":144.00000,"TagNo":62336.0,"Pcs":1.0,"Loc":"G0303"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\"","Size1":48.00000,"Size2":144.00000,"TagNo":67931.0,"Pcs":124.0,"Loc":"G0303"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\"","Size1":48.00000,"Size2":48.00000,"TagNo":68912.0,"Pcs":1.0,"Loc":"REM75"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\"","Size1":48.00000,"Size2":144.00000,"TagNo":69589.0,"Pcs":177.0,"Loc":"RECVG"},{"SoNo":119152.0,"SoLn":1.00,"ItemID":"7075SHA6016","ItemDesc":"7075-T6 ALCLAD SHEET .016\"","Size1":48.00000,"Size2":48.00000,"TagNo":76493.0,"Pcs":1.0,"Loc":"REM75"},{"SoNo":119152.0,"SoLn":2.00,"ItemID":"2024SHA3125M","ItemDesc":"2024-T3 ALCLAD SHEET .125\" MRS","Size1":48.00000,"Size2":144.00000,"TagNo":72070.0,"Pcs":12.0,"Loc":"H0108"},{"SoNo":119152.0,"SoLn":3.00,"ItemID":"177SH-016","ItemDesc":"T17/7 SHEET .016\"
AMS-5528","Size1":36.00000,"Size2":120.00000,"TagNo":70031.0,"Pcs":1.0,"Loc":"S0504"},{"SoNo":119152.0,"SoLn":3.00,"ItemID":"177SH-016","ItemDesc":"T17/7 SHEET .016\"","Size1":36.00000,"Size2":120.00000,"TagNo":76108.0,"Pcs":10.0,"Loc":""}]

Thanks!!

5
  • Hello Greg, print the "result" variable and see what it contains ? Commented Sep 26, 2013 at 18:41
  • 1
    Change this JSONArray jArray = new JSONArray(result); to JSONObject joj=new JSONObject(result); as it is not jsonArray Commented Sep 26, 2013 at 18:43
  • so what you posted should work since you get jsonarray now Commented Sep 26, 2013 at 20:07
  • But it doesn't work, and this is my frustration. While stepping into the code, the next line the debugger runs is in the JSONArray.class public JSONArray(java.lang.String json) throws org.json.JSONException { /* compiled code */ }. The variables window shows that the value for 'json' is the above array, and the value for 'this' is null. Commented Sep 27, 2013 at 14:23
  • @GregSaladino can you paste your updated code. i am sure you are doing wrong thatis why you get exception. [ represetns json arraynode and this { represents json object node Commented Sep 27, 2013 at 17:03

2 Answers 2

1

Change this

  JSONArray jArray = new JSONArray(result);

To

  JSONObject jobject = new JSONObject(result);

What you get is a JSONObject

{ // represents json object node
   "Accept-Language": "en-US,en;q=0.8",
   "Host": "headers.jsontest.com",
   "Referer": "http://stackoverflow.com/questions/19036091/android-jsonarray-nullpointerexception-cannot-evaluate-jsonarray-tostring",
   "User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36",
   "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}    

Also use a thread or Asynctask if you are getting json from a server. All network related operation should be done on a thread

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

Comments

0

What you are trying to parse is not an array it is just JSONObject.

Arrays in JSON are denoted with []. E.g. this would be an array:

[{"key": "value", "key2": "value2"}]

And this is the single object contained in it:

{"key": "value", "key2": "value2"}

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.