I have developed app which connects to server and receive JSON response .Till this it works fine.But then it puts in Hashmap and HashMap in ArrayList to display data.It gives error on while putting Hashmap in ArrayList.My LogCat is
06-03 16:23:10.540: E/AndroidRuntime(15457): FATAL EXCEPTION: AsyncTask #1
06-03 16:23:10.540: E/AndroidRuntime(15457): java.lang.RuntimeException: An error occured while executing doInBackground()
06-03 16:23:10.540: E/AndroidRuntime(15457): at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-03 16:23:10.540: E/AndroidRuntime(15457): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.lang.Thread.run(Thread.java:856)
06-03 16:23:10.540: E/AndroidRuntime(15457): Caused by: java.lang.NullPointerException
06-03 16:23:10.540: E/AndroidRuntime(15457): at com.example.project.MainActivity$LoadAllProducts.doInBackground(MainActivity.java:498)
06-03 16:23:10.540: E/AndroidRuntime(15457): at com.example.project.MainActivity$LoadAllProducts.doInBackground(MainActivity.java:1)
06-03 16:23:10.540: E/AndroidRuntime(15457): at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-03 16:23:10.540: E/AndroidRuntime(15457): ... 5 more
and my class is
public class asasa extends Activity {
//ListView listView;
Intent intent;
public int currentimageindex=0;
private ProgressDialog pDialog;
Timer timer;
TimerTask task;
ImageView slidingimage;
ProgressDialog progressDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
static final String URL = "http://172.20.55.175/android_connect/get_all_products.php";
private static final String TAG_SUCCESS = "success";
private TextView mStatusView;
private EditText searchEditText;
private static final String TAG_PRODUCTS = "products";
private static final String TAG_NAME = "name";
private static final String TAG_Description = "description";
private static final String TAG_URL = "url";
private static final String TAG_Price = "price";
ListView list;
LazyAdapter adapter;
// flag for Internet connection status
Boolean isInternetPresent = false;
// products JSONArray
JSONArray products = null;
// Connection detector class
ConnectionDetector cd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
LoadAllProducts il = new LoadAllProducts();
il.execute(URL);
}
else
{
// Internet connection is not present
// Ask user to connect to Internet
Toast.makeText(asasa.this, "No Internet Connection You don't have internet connection.", Toast.LENGTH_LONG).show();
}
}
public void longToast(CharSequence message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(asasa.TAG_NAME));
artist.setText(song.get(asasa.TAG_Description));
duration.setText(song.get(asasa.TAG_Price));
imageLoader.DisplayImage(song.get(asasa.TAG_URL), thumb_image);
return vi;
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
// creating new HashMap
ArrayList<HashMap<String, String>> productsList;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(asasa.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
HashMap<String, String> map = new HashMap<String, String>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(URL, "GET", params);
// Check your log cat for JSON reponse
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length()-1; i++) {
JSONObject c = products.getJSONObject(i);
String name = c.getString(TAG_NAME);
String description = c.getString(TAG_Description);
String URl = c.getString(TAG_URL);
String price = c.getString(TAG_Price);
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_Description, description);
map.put(TAG_URL, URl);
map.put(TAG_Price, price);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
}
} 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
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(asasa.this, productsList);
list.setAdapter(adapter);
}
}
public void onPause()
{
super.onPause();
if (progressDialog.isShowing()) {
progressDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}
line 498 is
productsList.add(map);
in protected String doInBackground(String... args)
i cant figure out error...why it is occuring.it is succesfully return json response but can put in hashmap
You haven't initialized your
ArrayList<HashMap<String, String>> productsList;
Initialize it before using.
ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> productsList;
This only declares productsList
. You never intialized it, so it's null
by defaut. You should initialize it:
ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String, String>>();
Also usually I recommend using interface in the declaration, so you can later switch interface implementation if needed without much headache. Also specifying minimum visiblity possible is a good OOP practice as it enforces encapsulation.
private List<Map<String, String>> productsList = new ArrayList<HashMap<String, String>>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With