Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Retrieving and Viewing Messages Creating a Custom List Adapter for Messages

message.getString(ParseConstants.KEY_FILE_TYPE) give null pointer exception. anything in that parameter = null

ParseObject message = mMessages.get(position);

    Log.d(TAG,"this is what get string gets: "+ message.getString("ParseConstants.KEY_FILE_TYPE"));
    //if(message.getString(ParseConstants.KEY_FILE_TYPE).equals(ParseConstants.TYPE_VIDEO)) {
        holder.iconImageView.setImageResource(R.mipmap.ic_action_picture);
    //}
    //else{ //if its a video
     //   holder.iconImageView.setImageResource(R.mipmap.ic_action_play_over_video);
    //}

    holder.nameLabel.setText(message.getString(ParseConstants.KEY_SENDER_NAME));
    return convertView;
}

private static class ViewHolder{
    ImageView iconImageView;
    TextView nameLabel;
}

}

like stated in the title, whatever key i use. the value of message.getString is null . I have gone so far as to copy ben's code from every file but the mainActivity file and it still gives me null..... has anyone come across this issue? could it be an issue with Parse.com? To clarify, my cloud has 5 items under the fileType key which are 3 images and 2 videos

I just observed this issue also. It seems the view holder becomes null after initialized once.

1 Answer

Found the bug.

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

    ViewHolder holder = new ViewHolder();

    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
        holder.iconImageView = (ImageView) convertView.findViewById(R.id.messageIcon);
        holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel);
        //set holder as tag
        convertView.setTag(holder);
    } else{
        holder = (ViewHolder)convertView.getTag();

    }

In the 'else' condition, the line: holder = (ViewHolder)convertView.getTag();

in the above line , calling "convertView.getTag()" without previously calling the corresponding "setTag()" in the 'if' condition.

Adding "convertView.setTag(holder);" after calling "convertView.findViewById(R.id.senderLabel);" in the 'if' condition should do the trick.