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

Fouchimi Ousmane
3,349 PointsNullPointerException
Hitting nullPointerException within MessageAdapter at line message.getString(ParseContants.KEY_FILE_TYPE).equals(ParseContants.TYPE_IMAGE));
This error make my application crashes. Any help will be appreciated. Thank you

Fouchimi Ousmane
3,349 PointsBelow is the code that i have inside MessageAdapter.
package com.mobileapps.blogsocial;
import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView;
import com.parse.ParseObject;
import java.util.List;
/**
-
Created by Android on 7/9/2015. */ public class MessageAdapter extends ArrayAdapter<ParseObject> {
public static final String TAG = MessageAdapter.class.getSimpleName();
protected Context mContext; protected List<ParseObject> mMessages;
public MessageAdapter(Context context, List<ParseObject> messages) { super(context, R.layout.message_item, messages); mContext = context; mMessages = messages; Log.d("Message initialized:", "" + mMessages); }
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder;
if(convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null); holder = new ViewHolder(); holder.iconImageView = (ImageView) convertView.findViewById(R.id.messageIcon); holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel); }else { holder = (ViewHolder) convertView.getTag(); } ParseObject message = mMessages.get(position);
/* Log.e(TAG, "Parse User: " + message); Log.e(TAG, "KEY_FILE_TYPE: " + ParseConstants.KEY_FILE_TYPE); Log.e(TAG, message.getString(ParseConstants.KEY_FILE_TYPE));*/ if (ParseConstants.TYPE_IMAGE.equals(message.getString(ParseConstants.KEY_FILE_TYPE))) { holder.iconImageView.setImageResource(R.drawable.ic_action_picture); } else { holder.iconImageView.setImageResource(R.drawable.ic_action_play_over_video); } holder.nameLabel.setText(message.getString(ParseConstants.KEY_SENDER_NAME));
return convertView;
}
private static class ViewHolder { ImageView iconImageView; TextView nameLabel; }
}

Fouchimi Ousmane
3,349 PointsHere is the Code i have inside InboxFragment class
package com.mobileapps.blogsocial;
import java.util.List;
import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter;
import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseObject; import com.parse.ParseQuery; import com.parse.ParseUser;
public class InboxFragment extends ListFragment {
protected List<ParseObject> mMessages;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inbox,
container, false);
return rootView;
}
@Override
public void onResume() {
super.onResume();
getActivity().setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId());
query.addDescendingOrder(ParseConstants.KEY_CREATED_AT);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> messages, ParseException e) {
getActivity().setProgressBarIndeterminateVisibility(false);
if (e == null) {
//We found messages!
mMessages = messages;
String[] usernames = new String[mMessages.size()];
int i = 0;
for(ParseObject message : mMessages) {
usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
i++;
}
MessageAdapter adapter = new MessageAdapter(
getListView().getContext(),
messages);
setListAdapter(adapter);
}
}
});
}
}
2 Answers

faraz
Courses Plus Student 21,474 PointsMake sure you check if the message != null, that may be your problem. Also, what are you trying to do here?
if(convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
holder = new ViewHolder();
holder.iconImageView = (ImageView) convertView.findViewById(R.id.messageIcon);
holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel);
}else {
holder = (ViewHolder) convertView.getTag();
}

Fouchimi Ousmane
3,349 Pointsfaraz i am simply trying to inflate the inboxFragment in that block of code. The first time it method getView() get called convertView will be null but once it get populated it won't be null anymore, and i will simply have to reuse it if the user ever scroll of the screen. That is why i'm doing that. And Ben has discussed it on the video as well.

faraz
Courses Plus Student 21,474 PointsTry downloading the source code from the "Teachers Notes" and comparing it to your code.
Seth Kroger
56,415 PointsSeth Kroger
56,415 PointsIt would be helpful if you could post your code, so we can see what's happening leading up to that line.