Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for null JSON Objects in Java/Android

I am getting this LogCat:

06-22 15:30:53.731: E/AndroidRuntime(2389): java.lang.NumberFormatException: Invalid float: "null"
06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.StringToReal.parseFloat(StringToReal.java:310)
06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.Float.parseFloat(Float.java:300)
06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.Float.valueOf(Float.java:337)

Here is code:

try
{
    jObject = new JSONObject(result);
    starAvg = jObject.getString("AverageRating"); 
}
ratingsBar = (RatingBar) findViewById(R.id.theRatingBar);
ratingsBar.setRating(Float.valueOf(starAvg));

Here is the context:

In PHP, I am averaging total numbers in a column in a MySQL table. When there are ANY rows, it will send back an average of the data in it and it encodes it, and Java picks it up as a JSON object. But sometimes, there are cases where a table may have 0 rows, so I get this JSON Object:

 {"AverageRating":null}

My app then crashes and the LogCat is as seen above.

The String doesn't seem to care if it picks up a Null JSON Object but the app crashes when I do Float.valueOf(theString).

Another side note, I have tried to test this way:

if String is Null, Float = 0 
if String is not null, Float.valueOF(String)

But it doesn't ever seem to read the String as null. Is it actually NOT null in this case?

like image 598
TheLettuceMaster Avatar asked Jun 22 '12 22:06

TheLettuceMaster


People also ask

How check JSON object is null or not in android?

Try with json. isNull( "field-name" ) .

How do I check if a JSON object is null?

Return valueJsonObject::isNull() returns a bool that tells if the JsonObject points to something: true if the JsonObject is null, false if the JsonObject is valid and points to an object.

Can a JSON object be null?

Null valuesJSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.

How do you handle null values in JSON response in Java?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.


2 Answers

Use the following method of JsonObject to check if a value against any key is null

public boolean isNull(java.lang.String key)

This method is used to check Null against any key or if there is no value for the key.

Use below snippet

if (jsonObject.isNull("AverageRating")) {
    Toast.makeText(this, "Cannot Convert!!", Toast.LENGTH_LONG).show();
    //float set to 0
} else {
    Float.valueOf(jsonObject.getString("AverageRating"));
}
like image 165
Vipul Avatar answered Oct 06 '22 01:10

Vipul


String str = jObject.getString("AverageRating");
float number = 0.f;
try
{
    number = Float.parseFloat(str);
}
catch (NumberFormatException e)
{
    number = 0;
}

parseFloat() will throw an exception if it receives null.

like image 20
nullpotent Avatar answered Oct 06 '22 01:10

nullpotent