I have a class which reads a JSON and sets it into a listview, but I have an error in the logcat when parsing the JSON array making it fail after that the asynctask
I tested the JSON and it's right, I don't know why does it says it's wrong
LogCat:
04-29 22:35:55.038 3120-3150/com.ivanmz.forotek.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.NullPointerException
at com.ivanmz.forotek.app.CaracteristicasProducto$LoadInbox.doInBackground(CaracteristicasProducto.java:85)
at com.ivanmz.forotek.app.CaracteristicasProducto$LoadInbox.doInBackground(CaracteristicasProducto.java:58)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
The Class:
public class CaracteristicasProducto extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> inboxList;
// products JSONArray
JSONArray inbox = null;
// Inbox JSON url
// ALL JSON node names
private static final String TAG_MESSAGES = "caracteristicas";
private static final String TAG_CARA = "caracteristica";
private static final String TAG_VALOR = "valor";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_producto);
// Hashmap for ListView
inboxList = new ArrayList<HashMap<String, String>>();
// Loading INBOX in Background Thread
new LoadInbox().execute();
}
/**
* Background Async Task to Load all INBOX messages by making HTTP Request
* */
class LoadInbox extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CaracteristicasProducto.this);
pDialog.setMessage("Loading Inbox ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Inbox JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
Intent intent = getIntent();
String id = intent.getExtras().getString("id");
// getting JSON string from URL
JSONObject json = jsonParser.makeHttpRequest("http://www.forotek.net/androidapp/caracteristicasproducto.php?id="+id, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Inbox JSON: ", json.toString());
try {
inbox = json.getJSONArray(TAG_MESSAGES);
// looping through All messages
for (int i = 0; i < inbox.length(); i++) {
JSONObject c = inbox.getJSONObject(i);
// Storing each json item in variable
String cara = c.getString(TAG_CARA);
String valor = c.getString(TAG_VALOR);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CARA, cara);
map.put(TAG_VALOR, valor);
// adding HashList to ArrayList
inboxList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
CaracteristicasProducto.this, inboxList,
R.layout.list_caracteristica_producto, new String[] {TAG_CARA, TAG_VALOR},
new int[] {R.id.from, R.id.subject});
// updating listview
setListAdapter(adapter);
}
});
}
}
}