Rahulkapil
Rahulkapil

Reputation: 294

index out of bound exception in arraylist

I am developing an application in which I have parsed Json of public facebook profile. I got String of imageurl and saved them into ArrayList and then implemented in listView. everything is going fine but sometimes my application is crashing by giving an error as "array index out of bound" or "memory space exception". unable to resolve this problem. help me out. thanks in advance.

I am attaching my code also.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = View.inflate(SocialActivity.this, R.layout.facebook_row, null);
    ImageView iv_user = (ImageView)row.findViewById(R.id.iv_user_pic);
    TextView tv_title, tv_description, tv_time;
    tv_title = (TextView)row.findViewById(R.id.tv_title);
    tv_description = (TextView)row.findViewById(R.id.tv_discription);
    tv_time = (TextView)row.findViewById(R.id.tv_time);
    Button btn_count = (Button)row.findViewById(R.id.btn_arrow);
    //bitmap = DownloadImage(alJson.get(position).strPicUrl);
    tv_title.setText(alJson.get(position).strName);
    tv_description.setText(alJson.get(position).strMessage);
    tv_time.setText(alJson.get(position).strDate);
    btn_count.setText(alJson.get(position).strCount);
    if((alJson.get(position).strPicUrl).equals(null){
        iv_user.setBackgroundResource(R.drawable.contact_img);
    } else{
        iv_user.setImageBitmap(al_bitmap.get(position));
    }
    return row;
}

Upvotes: 0

Views: 1856

Answers (3)

Vivek Mishra
Vivek Mishra

Reputation: 5705

Making position variable final solved my problem

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

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Maybe possible your ArrayList alJson is empty so check before using it as:

int listsize = alJson.size();
if(listsize>=0 && listsize<=position)
{
    tv_title.setText(alJson.get(position).strName);
    tv_description.setText(alJson.get(position).strMessage);
    tv_time.setText(alJson.get(position).strDate);
    btn_count.setText(alJson.get(position).strCount);

    if((alJson.get(position).strPicUrl).equals("null"){
     iv_user.setBackgroundResource(R.drawable.contact_img);
    }
    else{

    iv_user.setImageBitmap(al_bitmap.get(position));

    }
}
else
{
 //DO SOME CODE HERE
}

Upvotes: 0

Bojan Radivojevic
Bojan Radivojevic

Reputation: 737

Make sure the alJson.size() is not 0
and also try logging alJson.size() and position to see if there's something wrong with these values

Upvotes: 1

Related Questions