1

I was trying to use AsyncTask for lazy load of images in the adapter.

        public View getView(final int position,View convertView,ViewGroup parent){

    Bitmap userAvatarStream = null,podIconStream = null ;
    Bitmap podIconStream1 = null;
    Bitmap podIconStream2 = null;

    View v = convertView;

    if (v == null){
        LayoutInflater li = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.user_list_item,null);

    }

 user = users.get(position);    


    if(user != null){

        URL userImageURL,podImageURL = null;

        //TextView tk = (TextView)v.findViewById(R.id.text_key);
        TextView firstname = (TextView)v.findViewById(R.id.follower_fullname);
        ImageView user_avatar = (ImageView)v.findViewById(R.id.follower_user_avatar);

        new LoadImage(user_avatar).execute(); 

TextView userProfileClick = (TextView)v.findViewById(R.id.follower_fullname);

        userProfileClick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String uID = user.getID();


                //Pass User id to another Intent(UserProfile)
                Intent userIntent = new Intent(activity,UserProfileMainActivity.class);
                userIntent.putExtra("userID",uID);
                activity.startActivity(userIntent);

            }
            });         

        ListView podlist = (ListView)v.findViewById(R.id.user_list); 

        ArrayList<Cr> List = new ArrayList<Cr>();

        for(Crb c : user.crList){
            List.add(c);
        }


        //new LoadImage(user_avatar).execute(); 
        UserFollowingPodListAdapter podadapter = new UserFollowingPodListAdapter(activity, R.layout.user_crumbs_pod_list_item,crumbsUpList,activity);
        podlist.setAdapter(podadapter);

    }
    return v;
} 

class LoadImage extends AsyncTask<String, Void, String>{

    private ImageView imv;
    private String path;
Bitmap userAvatarStream = null ;
final Bitmap podIconStream = null;

ProgressDialog dialog;

    @Override 
    protected void onPreExecute(){
        //Setting all the variables by getting the ids from the layout


    return;

    }


    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub


        URL userImageURL,podImageURL = null;

        try {
            userImageURL = new URL(user.imageUrl);
            if (user == null)
                return null;
            userAvatarStream = BitmapFactory.decodeStream(userImageURL.openConnection().getInputStream());
            if (userAvatarStream == null)
                return null;




        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

     @Override

     protected void onPostExecute(String result){
         user_avatar.setImageBitmap(userAvatarStream);

         return;

     }

}

}

I got a Java Null Poitner Exception at onPostExecute.

I've debugged the code and userAvatarStream is not null. Have tried if/else, try/catch in trying to figure out where the null exception is happening, and figured out where: user_avatar.setImageBitmap(userAvatarStream); But, don't know why it is happening or how to remove it. Any help will be much appreciated.

Edit:

TextView firstname = (TextView)v.findViewById(R.id.follower_fullname);
            ImageView user_avatar = (ImageView)v.findViewById(R.id.follower_user_avatar);
5
  • 2
    user_avatar is null, it shouldn't be null. It seems no where you assigned any thing to user_avatar. Commented Feb 7, 2012 at 17:10
  • Where are you instantiating the "user_avatar" member? Post that code so that we can see where/when you're doing it. Could be helpful. Commented Feb 7, 2012 at 17:11
  • Have posted the user_avatar instantiating part. Commented Feb 7, 2012 at 17:16
  • Follow it down the line: are you certain that there's an ImageView with id follower_user_avatar in your layout? Commented Feb 7, 2012 at 17:21
  • Yes. And, the code works if I remove asynctask from the code and not use any threading Commented Feb 7, 2012 at 17:25

1 Answer 1

1

In agreement with the commentors, you need to actually instantiate the user_avatar for it to be usable. You open the stream to pull the avatar, but never actually do anything with it.

Edit:

View v = LayoutInflater.from(context).inflate(R.layout.mybigdamnlayout, null);

Use this over SystemService. It is much more effective (by effective I mean I have never had a NPE this way).

Sign up to request clarification or add additional context in comments.

5 Comments

@Hiccup When are you calling the task? In onCreate(Bundle)? Try in onResume(). I don't think that should matter, but it may.
This is an adapter and not an activity.
Better, could you just post the whole adapter.getView(...) code?
Don't use Context.getSystemService. Instead you somthing like what I posted above.
Did the change. Error still continues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.