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
Joan Marc Sanahuja
Courses Plus Student 12,926 PointsError when whereequalto does not find a result
Hi! I have an error when parseexception is NOT null. When I enter the username of an existing user, everything works fine. If the editText is empty, the alert works fine. But when I enter a username that doesn't exist an (I suppose) the ParseException is not null, the app stops. Here is the code:
public class ChallengeFragment extends Fragment {
protected EditText mEditText;
protected Button mButton;
protected List<ParseUser> mUsers;
protected TextView mFriendFound;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected ParseUser userObject;
public static final String TAG = LeaderboardFragment.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_challenge,
container, false);
mEditText = (EditText) rootView.findViewById(R.id.findAFriend);
mButton = (Button) rootView.findViewById(R.id.findAFriendButton);
mFriendFound = (TextView) rootView.findViewById(R.id.UserFound);
return rootView;
}
@Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mEditText.getText().toString();
username = username.trim();
if (username.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setMessage(R.string.login_error_message)
.setTitle(R.string.login_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo(ParseConstants.KEY_USERNAME, username);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
userObject = objects.get(0);
mFriendFound.setText((CharSequence) userObject.get(ParseConstants.KEY_USERNAME));
mFriendFound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFriendsRelation.add(userObject);
mCurrentUser.saveInBackground();
}
});
}
else {
// Something went wrong.
mFriendFound.setText("username does not exist");
mCurrentUser.saveInBackground();
}
}
});
}
}
});
}
}
11 Answers
Joan Marc Sanahuja
Courses Plus Student 12,926 PointsFinally I found the solution. May help to somebody:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo(ParseConstants.KEY_USERNAME, username);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
try {
userObject = objects.get(0);
mFriendFound.setText((CharSequence) userObject
.get(ParseConstants.KEY_USERNAME));
mFriendFound
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFriendsRelation
.add(userObject);
}
});
}
catch (IndexOutOfBoundsException r) {
mFriendFound.setText("Not found");
}
} else {
// Something went wrong.
Log.d("username", "Error: " + e.getMessage());
}
mCurrentUser.saveInBackground();
}
});
Steve Hunter
57,712 PointsHave you been into the debugger and stepped through this to see precisely what the error is and where it is caused? LogCat should help with that.
I don't fully understand what you are trying to achieve; it looks like you have a list of username in Parse and you're querying that list from a textView. So, you're then saying if the user is blank, the Alert works and if the user is found, the rest works, but if you search for a non-existent user, the back-end connection is throwing an error within the Java?
Is that about right?
Steve.
Joan Marc Sanahuja
Courses Plus Student 12,926 PointsYes, it's more or less what you said. The user enters a username and when the button is clicked:
- if the user leave the editText box blank, appears an alert to inform that he has to enter a valid username. It works.
- if the user enter a correct username, then he can add him as a friend clicking on the username that appears in the textView. It works.
- if the user enter an invalid username, that does not exist, then here comes the problem. Appears the message: "Unfortunately, myApp has stoppet!" and the it goes back to the mainActivity.
This is what I found in the logCat but I don't understand it:
09-16 19:48:31.703: E/AndroidRuntime(1695): FATAL EXCEPTION: main
09-16 19:48:31.703: E/AndroidRuntime(1695): Process: com.jm.codedown, PID: 1695
09-16 19:48:31.703: E/AndroidRuntime(1695): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
09-16 19:48:31.703: E/AndroidRuntime(1695): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
09-16 19:48:31.703: E/AndroidRuntime(1695): at java.util.ArrayList.get(ArrayList.java:308)
09-16 19:48:31.703: E/AndroidRuntime(1695): at com.jm.codedown.ChallengeFragment$1$1.done(ChallengeFragment.java:76)
09-16 19:48:31.703: E/AndroidRuntime(1695): at com.parse.FindCallback.internalDone(FindCallback.java:45)
09-16 19:48:31.703: E/AndroidRuntime(1695): at com.parse.FindCallback.internalDone(FindCallback.java:31)
09-16 19:48:31.703: E/AndroidRuntime(1695): at com.parse.Parse$5$1.run(Parse.java:891)
09-16 19:48:31.703: E/AndroidRuntime(1695): at android.os.Handler.handleCallback(Handler.java:733)
09-16 19:48:31.703: E/AndroidRuntime(1695): at android.os.Handler.dispatchMessage(Handler.java:95)
09-16 19:48:31.703: E/AndroidRuntime(1695): at android.os.Looper.loop(Looper.java:136)
09-16 19:48:31.703: E/AndroidRuntime(1695): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-16 19:48:31.703: E/AndroidRuntime(1695): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 19:48:31.703: E/AndroidRuntime(1695): at java.lang.reflect.Method.invoke(Method.java:515)
09-16 19:48:31.703: E/AndroidRuntime(1695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-16 19:48:31.703: E/AndroidRuntime(1695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-16 19:48:31.703: E/AndroidRuntime(1695): at dalvik.system.NativeStart.main(Native Method)
09-16 19:48:31.719: W/ActivityManager(544): Force finishing activity com.jm.codedown/.LeaderboardsActivity
09-16 19:48:32.723: W/ActivityManager(544): Activity pause timeout for ActivityRecord{52974500 u0 com.jm.codedown/.LeaderboardsActivity t2 f}
Steve Hunter
57,712 PointsLine 76 of ChallengeFragment is accessing an out-of-bounds index within an ArrayList.
Steve Hunter
57,712 PointsuserObject = objects.get(0); looks wrong to me ... that seems to be causing the LogCat issue.
Steve Hunter
57,712 PointsIs the (0) needed, or is it just ()?
Joan Marc Sanahuja
Courses Plus Student 12,926 PointsYes, it is needed. The findInBackground returns a list. You have to get the first item (the only item of the list). If you quit the 0, then there is a code error. It's strange because when the e==null and enters in this code part, everything works and I get the object I want, The crash comes when the exception is not null where userObject = objects.get(0) is not called.
Steve Hunter
57,712 PointsSorry, my phone seems to have dropped a reply there.
What's at line 76 in ChallengeFragment? That's causing the exception in LogCat as it is trying to access an ArrayList wrongly.
Joan Marc Sanahuja
Courses Plus Student 12,926 PointsDon't worry and thank you for your help.
Line 76 is userObject = objects.get(0). Maybe there's another way to get the result of the search... I don't know.
Steve Hunter
57,712 PointsWhat was the problem? I can't view the code list on the phone.
Joan Marc Sanahuja
Courses Plus Student 12,926 PointsI implemented a try{ } catch (IndexOutOfBoundsException r){ } This exception is the one that appears in the logCat and causes the error. When the user enters an username, if it exists, it is saved in objects. Then you can get that username by calling the first element (and the only one) of objects. The problem comes when no result is found. The app is trying to acces to an object that does not exist, and IndexOutOfBoundsException appears.
I missunderstood the way that findInBackground works. I thought that when no result is found, the ParseException is not null. But ParseException is for another type of errors and is null even you enter an invalid username.