Reputation: 2135
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);
}
Upvotes: 1
Views: 592
Reputation: 21
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");
}
}
Upvotes: 0
Reputation: 1188
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) {
...
}
Upvotes: 3