user642347
user642347

Reputation: 331

Java null pointer exception when i pass this three parameter to the lazy loading class

@Override
    public View getView(int position, View convertView, ViewGroup parent)   
    {
        ViewHolder holder;
        if(convertView == null)
        { 
            convertView = inflater.inflate(R.layout.photodata,null);

            myHolder = new ViewHolder();
            try
            {  
                myHolder.imgPhoto = (ImageView) convertView.findViewById(R.id.imgPhoto);            
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            convertView.setTag(myHolder);
        }
        else
        {
            myHolder = (ViewHolder)convertView.getTag();
        }
        System.out.println("String image is" + getItem(position).photo);
        myHolder.imgPhoto.setTag(getItem(position).photo);
        System.out.println("String image is" + getItem(position).photo);
        imageLoader.DisplayImage(getItem(position).photo,activity, myHolder.imgPhoto);

        return convertView;
    }

Error:

FATAL EXCEPTION: main
java.lang.NullPointerException
 at.com.android.adapter.PhotoAdater.getView(PhotoAdater.java:59)
 at.android.widget.AbsListView.obtainView(AbsListView.java:1315)

Upvotes: 0

Views: 466

Answers (3)

Jeeva
Jeeva

Reputation: 1155

This code you are declaring ,

 ViewHolder holder;

but you are using different object,

myHolder.imgPhoto= (ImageView) convertView.findViewById(R.id.imgPhoto);   

change as,

 holder.imgPhoto= (ImageView) convertView.findViewById(R.id.imgPhoto); 

and also change Layoutinflater like this,

convertView = inflater.inflate(R.layout.photodata,null,false);

this code reduced error

Upvotes: 1

arcdrag
arcdrag

Reputation: 114

My guess without line numbers and/or knowing what you're passing to this method...if you attempt to call this method with position == null then it will throw an NPE because you're passing null to a primitive.

Upvotes: 0

Balanivash
Balanivash

Reputation: 6867

There seems to be a problem in the code you have pasted here. Seeing the code I assume

  ViewHolder holder;

should actually be

  ViewHolder myHolder;

In that case, you have created the instance using new in the if condition, but in the else, it is not done. So ,myHolder points to null. Think this is the reason for the NullPointerException

Upvotes: 0

Related Questions