错误转换结果 java.lang.NullPointerException

Posted

技术标签:

【中文标题】错误转换结果 java.lang.NullPointerException【英文标题】:Error converting result java.lang.NullPointerException 【发布时间】:2014-06-23 23:00:18 【问题描述】:

我是使用 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 开发的任何提示。

【问题讨论】:

【参考方案1】:

我不知道究竟是什么在抛出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

一般来说,复杂的语句(就像你有的几个)使调试和快速识别错误比单独的语句在不同的行上更困难。将它们分开需要更多代码,但可以增强可读性和调试工作。

【讨论】:

嗯,这就是你在谷歌地图中使用地图标记的方式,我已经用过几次了,没有问题,唯一的一个是我使用的是 json 值而不是硬编码的值。 我不怀疑这是使用它的正确方法。将所有内容放在一个语句中会使跟踪 NullPointerExceptions 变得困难。我只是主张单独提取 JSON 值,然后构建 LatLng,构建图标,然后构建 MarkerOptions。在单独的语句中做这些,我怀疑你会很容易找到 NPE。 我试过了,但没有帮助。我相信问题出在其他地方,但由于某种原因,使用地图标记的线条之间存在问题。我转换了代码,问题在同一行,但中间 try 语句的第二个异常被抛出。更不用说我的新日志猫打印输出显示所有变量都是正确的。【参考方案2】:

这是一个新手错误,我尝试向尚未存在的地图添加标记。不敢相信我看过了。

刚把这条线移到顶部就可以了。

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

感谢您的帮助。

【讨论】:

以上是关于错误转换结果 java.lang.NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

疑难错误之结果类型转换

SwiftUI 错误:无法转换“UIImage”类型的值?关闭结果类型“无效”

错误转换结果 java.lang.NullPointerException

可捕获致命错误的 FindAll() 结果:DateTime 类的对象无法转换为字符串

Math.Round() 同时转换和计算返回错误的结果

"缓冲区错误", "错误转换结果 java.lang.NullPointerException: lock == null