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
Luke O'Neill
7,822 PointsRetrieving another users information Parse.com
Hey guys I am working on an app and I cannot access the information of another user from parse.com. I currently have all of your friends in a list view and when you click on the person it will toast their location. Location is just an extra field I added to the parse object, just like username, etc. Here is my code for retrieving the data.
@Override
public void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
// String user = mCurrentUser.getObjectId();
Object o = getListView().getItemAtPosition(position);
String user = (String) o;
final ParseObject newUser = mUser.getParseObject(user);
String objId = newUser.getObjectId();
query.getInBackground(objId, new GetCallback<ParseUser>() {
@Override
public void done(ParseUser user, ParseException e) {
if (e == null) {
// String str = (String)
// ParseUser.getCurrentUser().get("Location");
Object o = getListView().getItemAtPosition(position);
String str = (String) o;
String loc = (String) newUser.get("Location");
Toast.makeText(Friends.this, "Location of " + str + " is " + loc,
Toast.LENGTH_LONG).show();
ParseUser.getCurrentUser().saveInBackground();
}
}
});
I keep getting a null pointer exception on this line:
final ParseObject newUser = mUser.getParseObject(user);
Any help is greatly appreciated as I am stuck. Thanks!
1 Answer
Ben Junya
12,365 PointsHey dude,
You are ALMOST there.
Look in the Parse SDK docs - a ParseUser is a ParseObject, but a ParseObject isn't necessarily a ParseUser. ParseUser is a special type of ParseObject.
This line:
final ParseObject newUser = mUser.getParseObject(user);
Returns null because you're attempting to create a new ParseObject with ParseUser information.
So try this:
final ParseUser newUser = mUser.getParseUser(user);
String objID = newUser.getObjectId();
That could be one solution:
OR
It could be that there's an information mismatch in Parse.
Let's say that your friend's name is "Jake," and his username is "DrAwesome" If the list item is displaying the username, you're going to get the username in Object o. If the list item is displaying the name, you're going to get the name in Object o.
If you're trying to query a name to username, it's going to come up as null. Same thing vice versa.
You are currently trying to assign a value that is null in final ParseObject newUser - which means there's a mismatch of information.
Try running the debugger and putting some breakpoints in here. Look at the value of Object o and String user. Find out how that works.
I wish I could tell you the solution, but I don't know how your Parse is set up and what values there are.
Huge props for exploring on your own. Following the Treehouse tutorials is great for learning, but speaking from experience, making your own stuff will force you to learn so much more. You are on your way.
Luke O'Neill
7,822 PointsLuke O'Neill
7,822 PointsThanks for the answer man. I had tried the first one before and no luck. Unfortunately the name is also the username. I did some debugging and the Object o is in fact the name, so that is working. I think you're right that there is a mismatch of information, but I can't put my finger on what it is.
I would be happy to send you anything that would help us solve the problem. This is one of the last things I need to happen for everything to come together and I really want to get this to work.
Ben Junya
12,365 PointsBen Junya
12,365 PointsNo problem Luke. Try checking this:
what is the value of mUser? That might be making it null too.
Luke O'Neill
7,822 PointsLuke O'Neill
7,822 PointsI had mUser declared as
ParseUser mUser;which I then changed it tooprotected ParseUser mUser = new ParseUser();Now I am getting the null pointer exception on this line:
String objId = newUser.getObjectId();so what is null is definitelynewUser. I still could be screwing something up with how I declare it. I'm going to keep playing around with it, but thanks again for all your help. I really appreciate it.Ben Junya
12,365 PointsBen Junya
12,365 PointsYou're almost there. However, after looking a little deeper, I believe you need to use a new query. Make sure to read up on how to perform a query using the Parse documentation.
Just a rule of thumb - as you're learning to code, if you run into a problem, look into the official documentation first. If that doesn't work, then try the forum, and if that doesn't work, then go to StackOverflow - however, use caution with StackOverflow. There's lots of bad practices that seem cut/paste.
Take a look at the Parse docs here on queries: https://parse.com/docs/android_guide#queries - this is exactly what you need.
You don't need to put in a query for a ParseUser. All you really need here is the string that was collected from the ListView.
Erase your query, and then create a new query to search by a string. I'll explain line by line:
Ben Junya
12,365 PointsBen Junya
12,365 PointsHey dude, thanks for the points!
Feel free to hit me up on Google Hangouts. I started my Treehouse account about 6 months ago, and now I'm looking to get my first job as an Android dev. I had a ton of help from my friends when I started out. If you need help, just hit me up at bjunya@gmail.com - and before you ask, no, I won't charge you, ever.
Take care dude!