name is null - Custom ArrayAdapter

3248 views android
-1

I have a custom ArrayAdapter that is supposed to display a users' profile image and username.

I've been following this tutorial:

Custom Android Layouts with Your Own ArrayAdapter

Yet, when I open my activity, the app crashes and says:

    java.lang.NullPointerException: name is null

Which is true, since I have yet to build a feature for users to add one. Instead it should be displaying a placeholder image.(Picasso)

Here is my CustomAdapter:

public class ContactsAdapter extends ArrayAdapter<CustomContact>{

private Context context;
private List<CustomContact> contactList;

//constructor, call on creation
public ContactsAdapter(Context context, int resource, ArrayList<CustomContact> objects) {
    super(context, resource, objects);

    this.context = context;
    this.contactList = objects;

}

public View getView(int position, View convertView, ViewGroup parent) {
    //get the user we are displaying
    CustomContact customcontact = contactList.get(position);

    //get the inflater and inflate the XML layout for each item
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.mylist, null);



    ImageView image = (ImageView) view.findViewById(R.id.friend_icon2);
    TextView friend_name = (TextView) view.findViewById(R.id.Itemname);

    //get & set username
    String completeUsername = customcontact.getUsername();
    friend_name.setText(completeUsername);

    //get the image associated with this user
    int imageID = context.getResources().getIdentifier(customcontact.getImageURL(), "string", context.getPackageName());

    //set image
    Picasso.get().load(imageID).placeholder(R.drawable.placeholder_image).resize(96, 96)
            .centerCrop().into(image);


    return view;
    }

}

The line in question returning null is this one defining ImageID:

//get the image associated with this user
    int imageID = context.getResources().getIdentifier(customcontact.getImageURL(), "string", context.getPackageName());

Here is my ContactsActivity.java

answered question

I don't even see the name variable being used in the code. Where are you using that name variable? click on the nullpointerexception and see where it navigates you; possibly copy and paste the code of that class too.

That looks like a custom exception message... Can you show the full stacktrace?

1 Answer

6

To set image with Picasso you need to put an image url as parameter in the load() method but you are putting an integer id. You may try this

    Picasso.get().load(customcontact.getImageURL()).placeholder(R.drawable.placeholder_image).resize(96, 96)
        .centerCrop().into(image);

Please provide your ContactsActivity.java codes for further clarification.

posted this

Have an answer?

JD

Please login first before posting an answer.