Nelson John Nelson John - 3 months ago 14
Android Question

NullPointerException when downloading image

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

Answer

(ImageView) findViewById(R.id.imageViewID) 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;
    }

    @Override
    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(R.id.img); // here you get the image

        return rowView;
    }
}

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

Comments