转换结果java.lang.NullPointerException时出错

时间:2014-05-07 05:56:31

标签: android json database google-maps google-maps-markers

我是使用JSON的新手,我试图从我的服务器上的数据库获取数据,该数据库只有纬度和经度位置,以及用户名和时间戳。我现在不需要使用时间戳,而且我似乎遇到了一个我似乎无法解决的问题。

    String result = "";
    double Lat = 0;
    double Lng = 0;
    String title = "";
    try{

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("removed");
                    HttpGet("Removed");

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
                System.out.println("Test while");
            }
            is.close();

            result=sb.toString();
            try{
                System.out.println("Test 7, last pass");
                JSONArray jArray = new JSONArray(result);
                System.out.println("Test 8");
                for(int i=0;i<jArray.length();i++){
                    System.out.println("Test for");
                    JSONObject json_data = jArray.getJSONObject(i);
                    System.out.println("Test 9");
                    Lat= json_data.getDouble("Latitude");
                    System.out.println("Test 10 "+ Lat);
                    Lng = json_data.getDouble("Longitude");
                    System.out.println("Test 11 "+ Lng);
                    title= json_data.getString("User");
                    System.out.println("Test 12 "+ title );
                    mMap.addMarker(new MarkerOptions().position(new LatLng(Lat,Lng)).title(title).icon(BitmapDescriptorFactory.fromResource(R.drawable.zombie_marker)));
                    System.out.println("Test for end");
                }
            }catch(JSONException e){
                System.out.println("Test inner catch");
                Log.e("log_tag", "Error parsing data "+e.toString());
                  Log.e("log_tag", "Failed data was:\n" + result);
            }
        }catch(Exception e){
            System.out.println("Test middle catch");
            Log.e("log_tag", "Error converting result "+e.toString());
              Log.e("log_tag", "Failed data was:\n" + result);
        }
    }catch(Exception e){
        System.out.println("Test outer catch");
        Log.e("log_tag", "Error in http connection "+e.toString());
            Log.e("log_tag", "Failed data was:\n" + result);
    }
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    marker = mMap.addMarker(new MarkerOptions().position(home).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(home));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}

这是我正在使用的代码,这是我正在打印的日志猫我有一些打印输出,看看我在哪里。

    05-07 14:09:40.911: I/System.out(13775): Test while
    05-07 14:09:40.921: I/System.out(13775): Test 7, last pass
    05-07 14:09:40.921: I/System.out(13775): Test 8
    05-07 14:09:40.921: I/System.out(13775): Test for
    05-07 14:09:40.921: I/System.out(13775): Test 9
    05-07 14:09:40.921: I/System.out(13775): Test 10 31.767669
    05-07 14:09:40.921: I/System.out(13775): Test 11 -106.502012
    05-07 14:09:40.921: I/System.out(13775): Test 12 Zombie_1
    05-07 14:09:40.921: I/System.out(13775): Test middle catch
    05-07 14:09:40.921: E/log_tag(13775): Error converting result java.lang.NullPointerException
    05-07 14:09:40.921: E/log_tag(13775): Failed data was:
    05-07 14:09:40.921: E/log_tag(13775): [{"Longitude":"-106.502012","Latitude":"31.767669","User":"Zombie_1","Time":"2014-04-24 21:48:15"},{"Longitude":"0","Latitude":"0","User":"Zombie_0","Time":"2014-04-24 21:48:15"},{"Longitude":"-106.502012","Latitude":"31.767669","User":"Zombie_2","Time":"2014-04-24 21:49:52"},{"Longitude":"-106.507849","Latitude":"31.78073","User":"Zombie_3","Time":"2014-04-24 21:49:52"}]

所以我的PHP正在工作并向我发送我需要的数据,但我甚至看不到这个问题。这是我第一次使用JSON,所以任何帮助将不胜感激。或者有关从Android服务器上的数据库中读取数据的任何提示。

2 个答案:

答案 0 :(得分:0)

我不知道究竟是什么抛出了NullPointerException,但它正在这一行上发生:

mMap.addMarker(new MarkerOptions()
.position(new LatLng(json_data.getDouble("Latitude"),json_data.getDouble("Longitude")))
.title(json_data.getString("User"))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.zombie_marker))); 

将该行拆分为多个语句,更新您的异常日志记录以包含堆栈跟踪,然后再次运行代码。如果告诉他们记录Exception对象,大多数日志记录框架将打印堆栈跟踪。如果不是,可以直接使用e.printStackTrace()将堆栈跟踪打印到STDERR。这应该告诉你复杂语句的哪一部分抛出了NullPointerException

一般而言,复杂的语句(如您所拥有的几个语句)使得调试和快速识别错误比单独的语句在单独的行上更难。分离它们的代码更多,但它可以增强可读性和调试工作。

答案 1 :(得分:0)

这是一个新手错误我试图将标记添加到尚未存在的地图中。不敢相信我看了那个。

只需将此行移至顶部即可正常工作。

mMap =((MapFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap();

感谢您的帮助。