HTG
HTG

Reputation: 584

Parse File Query Not Working

I am attempting to get a file from Parse through a query. Parse is very vague on how to get files from the cloud, but I have managed to create a basic query to attempt to get a file

ParseQuery query = ParseUser.getQuery();
                    query.getInBackground(objects.get(i).getObjectId(), new GetCallback<ParseObject>() {

                        public void done(ParseObject object, ParseException e) {

                            ParseFile fileObject = (ParseFile) object.get("picture");
                            fileObject.getDataInBackground(new GetDataCallback() {

                                public void done(byte[] data, ParseException e) {
                                    if (e == null) {
                                        Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

                                        ImageView image = (ImageView) findViewById(R.id.profilePicture);

                                        image.setImageBitmap(bmp);

                                    } else {
                                        Toast.makeText(getApplicationContext(), "Image Not Found", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    });

The code above gives me a NullPointerException on the line:

ParseFile fileObject = (ParseFile) object.get("picture");

Need help getting the file through this method. Or is there an easier way to get a file from the Parse Cloud? All help is appreciated.

Upvotes: 2

Views: 1275

Answers (2)

ameron32
ameron32

Reputation: 136

In my experience, a null pointer exception on line containing the following:

ParseFile fileObject = (ParseFile) object.get("picture");

tells me that object is null during the attempt to access the get() method. This tells me that your query is either malformed or has no matching objects on the server, more likely the second.

I don't have any information regarding the data structure of your Parse cloud, so let me give you an example instead.

If you have a regular old user, and his Parse ObjectId is xyz123ab and he has a picture file under the key/column profilePicture. You have a User containing a ParseFile containing picture data, or [User [File [PictureData]]].

Here's how you would retrieve the above in code:

String objectIdOfUser = "xyz123ab";
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(objectIdOfUser, new GetCallback<ParseUser>() {
  @Override
  public void done(ParseUser parseUser, ParseException e) {
    if (e == null) {
      ParseFile picture = parseUser.getParseFile("profilePicture");
      if (picture == null) {
        // no parseFile in column "profilePicture" in this user.
        return; // there will be no data to retrieve
      }

      picture.getDataInBackground(new GetDataCallback() {
        @Override
        public void done(byte[] data, ParseException e) {
          if (e == null) {
            if (data.length == 0) {
              // data found, but nothing to extract. bad image or upload?
              return; // failing out
            }

            // SUCCESS
            // convert data and display in an imageview
          } else {
            // ParseFile contained no data. data not added to ParseFile?
          }
        }
      });
    } else {
      // no users had this objectId
    }
  }
});

Change ParseUser to ParseObject in the following if you aren't using a ParseUser. Don't forget to change the string "ObjectName" to your object.

// ... etc
ParseQuery<ParseObject> query = ParseObject.getQuery("ObjectName");
query.getInBackground(objectIdOfObject, new GetCallback<ParseObject>() {
  @Override
  public void done(ParseObject object, ParseException e) {
    if (e == null) {
      ParseFile picture = object.getParseFile("profilePicture");
      // etc...

Also, be certain that your data is loaded into the server properly. This can be tricky to determine on the web-client for Parse, unfortunately. Uploading an image properly is similar to this but in reverse, and an answer for another day.

Please review your code to get an objectId as shown here:

objects.get(i).getObjectId()

to determine if you are indeed accessing the objectId you thought you were, perhaps with a log or toast or debug breakpoint. That could be were everything starts to break down.

Upvotes: 4

Timothy Walters
Timothy Walters

Reputation: 16874

Your code looks fine, you simply need to check if the current row has a file reference in it's picture column. If the column is empty then you will obviously get a null reference when reading it out as a ParseFile.

Upvotes: 0

Related Questions