For some reason I'm getting a NumberFormatException, saying "null" is not a valid float. I don't understand how that is possible, because I even test for it beforehand:
This way I put the float value into a jsonObject:
JSONObject jsonNew = new JSONObject();
try {
jsonNew.put("due_date", task.getDueDate().getTime());
jsonNew.put("checked", task.isDone());
jsonNew.put("name", task.getName());
jsonNew.put("priority", task.getPriority());
jsonNew.put("due_time", task.getDueTime());
jsonNew.put("notification_before", task.getNotificationBefore());
jsonNew.put("id", task.getId());
jsonNew.put("proximity_alert", task.isAlertOn());
if (task.isLocationSet()) {
jsonNew.put("lon", task.getLongitude()+"");
jsonNew.put("lat", task.getLatitude()+"");
} else {
jsonNew.put("lon", null);
jsonNew.put("lat", null);
}
return jsonNew;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
I am using a String here, as I learned that otherwise there might be loss when using a JSONObject. Then I try to get it afterwards:
float lat = 0, lon = 0;
if((jsonNew.getString("lat"))!=null && (jsonNew.getString("lon"))!= null) {
Log.d("lat = ", jsonNew.getString("lat"));
lat = Float.parseFloat(jsonNew.getString("lat"));
lon = Float.parseFloat(jsonNew.getString("lon"));
locationSet = true;
}
Log even says that it is null, but why would it even go in then? It is not my first time programming and it really bums me out that I cannot even write an easy if-statement. Can someone help out?