1

I'm trying to retrieve the image from Parse.com. In DataBrowser, if the image file is empty its crashing in code. so I'm handling this error by checking if file!=null.

Its crashing at this line ParseFile file = (ParseFile) ob.get("image"); saying JSONObject cannot be cast to ParseFile.

So How to handle if Parse File is empty??

for (ParseObject ob : result) {
String perf = ob.getString("info");
ParseFile file = (ParseFile) ob.get("image");
if (file != null) {
    image_url = file.getUrl();
    } else {
         /*load some default image url*/
        image_url = "www.abc.com/image.png";
    }
    Picasso.with(getActivity()).load(image_url).into(imageView);
    textView.setText(perf);
    layoutCards.setVisibility(View.VISIBLE);
}
1
  • Get(image) is return null. Nothing to cast. Commented Jul 16, 2014 at 8:40

2 Answers 2

3

Try:

(Make sure in parse databrowser there is a field called "image" where you store parsefile)

ParseFile parseFile = ob.getParseFile("image");

then check:

if (parseFile != null && parseFile.getUrl() != null && parseFile.getUrl().length() > 0) {
            ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Knock Knock!!Thanks for figuring out Dexter. In older versions of Parse I was using object.get("image"). In Parse-1.5.1.jar its object.getParseFile("image");
0

How about catch the NullPointerException like this :

for(ParseObject ob : results) {
    try {
        ParseFile imageFile = (ParseFile) ob.get("image");

        imageFile.getDataInBackground(new GetDataCallback() {
            public void done(byte[] data, ParseException e) {
                if (e == null) {
                    // data has the bytes for the image
                    Log.d(TAG,"get image");
                } else {
                    // something went wrong
                }
            }
       });
    } catch (NullPointerException e) {
        Log.d(TAG,"got no image");
    }
}

1 Comment

I put a image file to my parse db then I delete the image file, (but the raw is still exist, just the image file is deleted) and these code works on this circumstance.

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.