2

I am trying to retrieve a new column called "address" I created in Parse User in my android app. But it returns me nullPointerException error and returns me nothing although I filled the address in the table.
This link only shows the way to store your custom field in the user table but I need to retrieve what I stored there.
This is my code:

ParseUser pUser = ParseUser.getCurrentUser();
userAddress.setText(pUser.getString("address").toString());

I tried get("address") as well but still it returns me nothing. Is there something I'm missing here?

3 Answers 3

2

Alright, I found the answer on my own. It turns out that Parse caches the ParseUser.getCurrentUser() object locally and the reason I wasn't able to get the data from server was because I changed the data on server and the client cache wasn't updated. I was able to fix this by fetching the ParseUser object from the server:

ParseUser.getCurrentUser().fetchInBackground();

and then after the object is retrieved from the server I was able to get the address field on my client.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to call it using a Query, then display it in a textView/editText. Try the following:

final ParseQuery<ParseObject> address = ParseQuery.getQuery("//Class Name");
    address.getFirstInBackground(new GetCallback<ParseObject>() {

        public void done(ParseObject reqAdd, ParseException e) {

            if (address != null) {
                Log.d("quizOne", "Got it");
                //Retrieve Age
                String //CreateNewString = reqAdd.getString("//Column name");
                TextView //textView Name = (TextView) findViewById(R.id.//textView ID);
                //textViewName.setText(//StringName);
                Toast.makeText(getApplicationContext(),
                        "Successfully Recieved Address",
                        Toast.LENGTH_LONG).show();


            } else {
                Log.d("//EnterName", "//Enter Error Message");
                Toast.makeText(getApplicationContext(),
                        "Can't receive address", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });

2 Comments

Can't I just use the normal ParseUser object methods? This code I guess should work. Ill try that and Ill let you know.
You could do. This method adds it into a textView/editText which I find easier to display on my application.
-1

Short answer: that user probably just doesn't have an address set.

Long answer: your code snippet will throw exceptions often, and you should expect and handle those, or use tests to avoid throwing them.

Read this page: http://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException

Key example/excerpt:

Object obj = null;
obj.toString();  // This statement will throw a NullPointerExcept

So pUser.getString("address") appears correct. But calling .toString() on the result requires you to be try/catching the exception. Maybe do

ParseUser pUser = ParseUser.getCurrentUser();
if (pUser.getString("address") != null) {
    userAddress.setText(pUser.getString("address"));
}

BTW, I believe the error is "nullPointerException" fyi! :)

2 Comments

Yea. My bad.. its nullPointerException. I edited that. Its not about the nullPointerException error. It returns nothing from my User table. And My user table has an address field and all the records have data. Its I think something about parse or maybe the way I am trying to do this is not correct.
The question is not about NPE, it's about why the author can't retrieve a custom field value from the User

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.