Reputation: 901
I have a database and a table in which there are two double columns:
The columns are allowed to be null because of some inner logic I'm using. I receive the data from the database in JSON form.
Where it occurred:
I experience an issue when there's null in these columns.
The code that caused the error -
Double longitude = myJSONObject.getDouble("Longitude");
And the error it caused -
Value null at Longitude of type org.json.JSONObject$1 cannot be converted to double
What I tried:
I used Double
and not double
, so I took a look into the java code and found out that JSONObject.getDouble()
returns a primitive double
, and there's no other suitable method that returns Double
.
So I tried instead using JSONObject.get()
which returns an Object
, and then cast it to Double
as so -
Double Longitude = (Double)myJSONObject.get("Longitude ");
But that raised the following error -
Cannot cast 'org.json.JSONObject$1' to 'java.lang.Double'
.
And I found out Object
cannot be cast to Double
.
Possible solution:
The only way I see that can work is getting the value as a String
and parsing it, but that seems inefficient. Is there any better way to receive a non primitive Double
from the JSON Object?
As always, I appreciate any help provided. (If something is unclear - please notify me and I'll do my best to be more coherent. I'm trying to improve in that field.)
Edits: requested information
Generating the JSON: Android
private class GetAllEvents extends AsyncTask<Void, Void, JSONArray> {
@Override
protected JSONArray doInBackground(Void... params) {
try
{
URL myUrl = new URL("http://xx.xx.xxx.xxx/GetAllEvents.php");
HttpURLConnection request = (HttpURLConnection)myUrl.openConnection();
request.setChunkedStreamingMode(0); //Probably default, just in case
if (request.getResponseCode() != 200) //If request reached errors
return null;
InputStream stream = request.getInputStream();
String jsonStr = convertStreamToString(stream);
if (jsonStr.equals("[]")) //Empty array
return null;
return new JSONArray(jsonStr);
}
catch (Exception e)
{
Log.w("myApp", e.toString());
}
return null;
}
}
Generating the JSON: PHP (on distant server)
<?require_once('MysqliDb.php');
$db = new MysqliDb();
//All closest events by date
$All = $db->query("SELECT * FROM Events;");
//Return in JSON
echo json_encode($All);
This generates a JSONArray, from which I extract the JSONObject in a for loop.
JSON Result example:
[{"ModifiedOn":"2015-03-30 14:28:21", "Name":"בטיזאדו", "Description":"חברי", "Phone":"0500000000", "Type":0, "Address":"הסטודיו", "Password":"123456", "ID":1, "Latitude":null, "Organiser":"דפנה", "Longitude":null, "City":"Tzur yigal"}]
(Please ignore the Hebrew)
And from here:
for (int i = 0; i < eventsArray.length(); i++) {
JSONObject myJSONObject = myJSONArray.getJSONObject(i);
--- *Do other things* ---
Double Longitude = (Double)myJSONObject.get("Longitude");
Double Latitude = (Double)myJSONObject.get("Latitude");
}
I hope that's clearer.
Upvotes: 1
Views: 11563
Reputation: 901
Credits go to @mkrakhin. Worked like a charm.
Double longitude = myJSONObject.isNull("Longtitude") ? null : myJSONObject.getDouble("Longtitude");
Upvotes: 2