I am trying to display an image from the Internet to the ImageView
but getting NullPointerException
.
error log:
java.lang.NullPointerException at com.example.user.labone.MainActivity$DownloadImageTask.onPostExecute(MainActivity.java:176)
code:
public class MainActivity extends ActionBarActivity {
String jsonString;
private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_TITLE = "title";
private static final String TAG_IMG= "src";
private static final String TAG_DESC ="desc";
JSONArray dataArray = null;
ArrayList<HashMap<String, String>> dataArrayList;
ListView list;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
//imageView=(ImageView) findViewById(R.id.imageView);
dataArrayList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(jsonString);
dataArray = jsonObj.getJSONArray(TAG_RESULTS);
for(int i = 0; i< dataArray.length(); i++){
JSONObject c = dataArray.getJSONObject(i);
String idString = c.getString(TAG_ID);
String titleString = c.getString(TAG_TITLE);
String imgString = c.getString(TAG_IMG);
String descString = c.getString(TAG_DESC);
HashMap<String,String> dataHashMap = new HashMap<String,String>();
dataHashMap.put(TAG_ID,idString);
dataHashMap.put(TAG_TITLE,titleString);
dataHashMap.put(TAG_IMG,imgString);
dataHashMap.put(TAG_DESC,descString);
// show The Image
new DownloadImageTask((ImageView) findViewById(R.id.imageViewID)).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
Log.i("TAG", imgString);
dataArrayList.add(dataHashMap);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, dataArrayList, R.layout.list_item,
new String[]{TAG_ID, TAG_TITLE, TAG_DESC},
new int[]{R.id.id, R.id.title, R.id.desc}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://geekysoft.com.pk/labone/labone_conn.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
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();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
jsonString =result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
@Override
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
Log.i("BITMAP", "BITMAP SIZE IS : " + mIcon11.getByteCount());
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}