0

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?

3 Answers 3

4

jsonNew.getString("lat") returns the String "null" and not null. Use !jsonNew.isNull("lat") etc. for the checks.

Sign up to request clarification or add additional context in comments.

1 Comment

Of course! Thank you! It took me so long already! It's so hard to find such easy things sometimes!
1

Try to replace this peace of code,hope this will help you to solve your problem.

if((jsonNew.getString("lat"))!=null && (jsonNew.getString("lon"))!= null && !jsonNew.getString("lat").equalsIgnoreCase("null") && !jsonNew.getString("lon").equalsIgnoreCase("null")) {
    Log.d("lat = ", jsonNew.getString("lat"));
    lat = Float.parseFloat(jsonNew.getString("lat"));
    lon = Float.parseFloat(jsonNew.getString("lon"));
    locationSet = true;
}

OR

 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()+"");
     }
    return jsonNew;
  } catch (JSONException e) {
     e.printStackTrace();
  }
  return null;


if(jsonNew.has("lat") &&  jsonNew.has("lon") && (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;
 }

Comments

0

Its perfectly valid that your getting the NumberFormatException. As the parseFloat java document it clearly says that if parse string is not valid then NumberFormat Exception will be thrown. In integer or float or double null is not value.

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.