0

I get this this nice message when the companyTwo value in my datastore is blank:

09-09 15:13:52.927: W/System.err(31091): org.json.JSONException: No value for companyTwo

What would be a good way to handle this assuming I can only pull from the datastore instead of post a filler value?

private static final String TAG_COMPANY ="company";
private static final String TAG_OTHERCOMPANY ="companyTwo";


String company1 = jsonObj1.getString(TAG_COMPANY);
String othercompany1 = jsonObj1.getString(TAG_OTHERCOMPANY);
5
  • you should check if it is null Commented Sep 9, 2013 at 19:27
  • @Hi-TechKitKatAndroid The JSON item companyTwo is in some contacts and missing in others. So it will work fine if companyTwo is in the JSON, but if not I get the JSON error. I have no way of telling if an entry will have the value of companyTwo or not. Commented Sep 9, 2013 at 19:33
  • 1
    JSON only supports trying and then catching if you use get function Commented Sep 9, 2013 at 19:47
  • otherwise use opt function and then check for null Commented Sep 9, 2013 at 19:48
  • @Hi-TechKitKatAndroid I used the opt function. Works good. Thanks! Commented Sep 9, 2013 at 19:50

1 Answer 1

1
try
{
String company1 = jsonObj1.getString(TAG_COMPANY);
}
catch(Exception ex)
{
// do whatever you want
}

alternatively you can do this

String company1 = String.valueOf(jsonObj1.optString(TAG_COMPANY));
Sign up to request clarification or add additional context in comments.

Comments

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.