Nelson John Nelson John - 1 year ago 58
Android Question

NullPointerException when downloading image

I am trying to display an image from the Internet to the

but getting

error log:

java.lang.NullPointerException at com.example.user.labone.MainActivity$DownloadImageTask.onPostExecute(


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;

protected void onCreate(Bundle savedInstanceState) {

list = (ListView) findViewById(;
//imageView=(ImageView) findViewById(;

dataArrayList = new ArrayList<HashMap<String,String>>();

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>();


// show The Image
new DownloadImageTask((ImageView) findViewById("");
Log.i("TAG", imgString);


ListAdapter adapter = new SimpleAdapter(
MainActivity.this, dataArrayList, R.layout.list_item,
new String[]{TAG_ID, TAG_TITLE, TAG_DESC},
new int[]{,,}


} catch (JSONException e) {


public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{

protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("");

// 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;

protected void onPostExecute(String result){
jsonString =result;
GetDataJSON g = new GetDataJSON();

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new;
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
Log.i("BITMAP", "BITMAP SIZE IS : " + mIcon11.getByteCount());
return mIcon11;

protected void onPostExecute(Bitmap result) {

Answer Source

(ImageView) findViewById( is returning null

You need a custom Adapter that should look like the following but please consider this example just as a way to show you the kind of things you need to do: there are better way to do it like using the ViewHolder pattern.

public class ListWithImageAdapter extends ArrayAdapter<String>{

    private final Context mContext;
    private final String[] mTexts;

    public ListWithImageAdapter(Context context, String[] texts) {
        super(context, R.layout.list_item_payment_type, texts);
        mContext = context;
        mTexts = texts;

    public View getView(int position, View view, ViewGroup parent) {
        View rowView= LayoutInflater.from(mContext).inflate(R.layout.list_item, null, true);

        ImageView imageView = (ImageView) rowView.findViewById(; // here you get the image

        return rowView;

Have you heard about Picasso and Glide? Please Google it...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download