2

I want to get json objects from a webservice and display them in a list view,

this is my code :

public class MainActivity extends Activity {

    private JsonObject data = null;
    private JsonObject response = null;
    private JsonArray records = null;
    private JsonObject record = null;
    private ArrayList<SearchResults> results = null;
    SearchResults sr1 = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<SearchResults> searchResults = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.ListView01);
        lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private ArrayList<SearchResults> GetSearchResults() {
        results = new ArrayList<SearchResults>();       
        new GetResults().execute("");

        return results;
    }

    private class GetResults extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            Map<String, String> callArgs = new HashMap<String, String>(1);

            callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");

            try {
                response = EventPulseCloud.call("ListEvents", callArgs);
            } catch (HttpClientException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JsonException e) {
                e.printStackTrace();
            }

            return response.get("Type").toString();
        }

        protected void onPostExecute(String result) {

            if(result.equals("success")) {

                data = (JsonObject) response.get("Data");
                records = (JsonArray) data.get("Records");

                try {
                    records = response.getObject ("Data").getArray ("Records");
                } catch (JsonException e) {
                    e.printStackTrace();
                }

                for(int i = 0; i < records.count(); i ++) {
                    record = (JsonObject) records.get(i);

                    sr1 = new SearchResults();
                    sr1.setAddress(record.get("address").toString());
                    sr1.setStartingDate(record.get("StartingDate").toString());
                    sr1.setTicketCategories(record.get("ticketCategories").toString());
                results.add(sr1);

                }

            }
        }

    }

}

and I get this in the logcat :

06-05 15:53:39.765: E/AndroidRuntime(10012): FATAL EXCEPTION: AsyncTask #1
06-05 15:53:39.765: E/AndroidRuntime(10012): java.lang.RuntimeException: An error occured while executing doInBackground()
06-05 15:53:39.765: E/AndroidRuntime(10012):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.lang.Thread.run(Thread.java:856)
06-05 15:53:39.765: E/AndroidRuntime(10012): Caused by: java.lang.NullPointerException
06-05 15:53:39.765: E/AndroidRuntime(10012):    at com.example.listview.MainActivity$GetResults.doInBackground(MainActivity.java:73)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at com.example.listview.MainActivity$GetResults.doInBackground(MainActivity.java:1)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-05 15:53:39.765: E/AndroidRuntime(10012):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-05 15:53:39.765: E/AndroidRuntime(10012):    ... 5 more
06-05 15:53:40.050: I/Process(10012): Sending signal. PID: 10012 SIG: 9

In the logcat, the line : MainActivity.java:73 is :

return response.get("Type").toString();

Everything seems correct to me... Please, do you have any idea about this ? Thank you :)

EDIT : when I try this code in java :

Map<String, String> callArgs = new HashMap<String, String>(1);
    callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
    response = EventPulseCloud.call("ListEvents", callArgs);

    if(response.get("Type").equals("success")) {
         JsonArray records = response.getObject ("Data").getArray ("Records");
         for (int i = 0; i < records.count (); i++) {
              JsonObject record = (JsonObject)records.get (i);
              System.out.print(record.get("ticketCategories").toString());
         }
    }
    else
        System.out.println("No");

and it displays on screen :

["SingleDay"] ["SingleDay"] ["Regular Ticket"]

it does mean that it returns something.

1
  • response is null so this line response = EventPulseCloud.call("ListEvents", callArgs); is probably returning null. I would check if that is the case Commented Jun 5, 2013 at 15:11

3 Answers 3

1

I'm guessing that the result of response.get("Type") is null. You should really check the return of that before trying to do something with it.

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

2 Comments

response itself is null or the exception would be in onPostExecute(), I believe
Thank you :) I have added a specification in the question can you check my edit please ?
1

response.get("Type") is returning null. Make sure you are are looking for the correct thing (maybe it's "type" instead of "Type"?) and, if nothing else, add some null-checking.

Object typeObj = response.get("Type");
if (typeObj != null) {
    return typeObj.toString();
}
return null;

3 Comments

response could also be null.
Without knowing what web services you are calling or what the response should look like, the only discernable thing to any of us here is that you have a NullPointerException, which happens when you try to access a member variable or call a method on an object that is not initialized. The rest is up to you to figure out.
Sorry, It was a little problem I've solved it and it works now but I got another problem : stackoverflow.com/questions/16948937/… can you check it please ?
0

Maybe the request for permission to access internet is missing. to avoid it. In Android Manifest file add

<uses-permission android:name="android.permission.INTERNET" />

Comments

Your Answer

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