null - Android getting JSONObject Exception -
i'm getting jsonobject exception in cases twitter value in json object media null in below code, though check make sure i'm not assigning value if is. way checking correct? or how can avoid code throwing exception , allow me check if twitter value there or not?
if(nextactivityobject.getjsonobject("media") != null){ media = nextactivityobject.getjsonobject("media"); log.d("next media object not empty", media.tostring()); if(media.getstring("twitter") != null && media.getstring("facebook") != null){ log.d("both not null", "both not null"); twitter = media.getstring("twitter"); facebook = media.getstring("facebook"); intent.putextra("twitter", twitter); intent.putextra("facebook", facebook); } else if(media.getstring("twitter") != null){ twitter = media.getstring("twitter"); log.d("just twitter not null", twitter.tostring()); intent.putextra("twitter", twitter); } else if(media.getstring("facebook") != null){ facebook = media.getstring("facebook"); log.d("just facebook not null", facebook.tostring()); intent.putextra("facebook", facebook); } }
you can use media.optstring("twitter");. , need media.has("twitter") distinguish between "no key called twitter" , "twitter key has empty string value".
to sum up, "twitter" key, write
else if(media.has("twitter")){ twitter = media.optstring("twitter"); // here twitter non-null string log.d("just twitter not null", twitter.tostring()); intent.putextra("twitter", twitter); }
Comments
Post a Comment