代码之家  ›  专栏  ›  技术社区  ›  lisa

NumberFormatException:无效浮点“null”(Android编程,JSONObjects)

  •  0
  • lisa  · 技术社区  · 12 年前

    由于某种原因,我收到了NumberFormatException,表示“null”不是有效的浮点值。我不明白这是怎么可能的,因为我甚至事先测试过:

    通过这种方式,我将float值放入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;
    

    我在这里使用的是String,因为我了解到,否则在使用JSONObject时可能会丢失。 然后我试着在事后得到它:

    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;
                }
    

    日志甚至说它是空的,但为什么它会进入呢?这不是我第一次编程,我甚至无法编写一个简单的if语句,这真的让我很恼火。 有人能帮忙吗?

    3 回复  |  直到 12 年前
        1
  •  4
  •   cygery    12 年前

    jsonNew.getString("lat") 返回字符串 "null" 而不是 null 使用 !jsonNew. isNull ("lat") 等进行检查。

        2
  •  1
  •   Hareshkumar Chhelana    12 年前

    尝试替换这种代码的平静,希望这将帮助您解决问题。

    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;
    }
    

    或

     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;
     }
    
        3
  •  0
  •   Sush    12 年前

    得到NumberFormatException是完全有效的。作为parseFloat java文档,它明确表示,如果解析字符串无效,则将抛出NumberFormat异常。在整数、浮点或双精度中,null不是值。