java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int java.util.List.size()'

时间:2015-09-02 23:15:38

标签: android

我是Java和Android Studio的新手。

我正在使用此示例代码

http://javapapers.com/android/draw-path-on-google-maps-android-api/

我的项目编译并运行到以下几点。我尝试过调试模式,我可以看到谷歌地图显示,并显示3个标记。然后我收到以下消息。

9-03 00:06:04.459  19022-19022/nyc.mymaptest3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: nyc.mymaptest3, PID: 19022
    java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
            at nyc.mymaptest3.PathGoogleMapActivity$ParserTask.onPostExecute(PathGoogleMapActivity.java:128)
            at nyc.mymaptest3.PathGoogleMapActivity$ParserTask.onPostExecute(PathGoogleMapActivity.java:101)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5938)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

我觉得返回的路由是空的/ null,我不知道如何进一步调试它。

第101行从这里开始>>>

private class ParserTask extends
            AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

        @Override
        protected List<List<HashMap<String, String>>> doInBackground(
                String... jsonData) {

            JSONObject jObject;
            List<List<HashMap<String, String>>> routes = null;

            try {
                jObject = new JSONObject(jsonData[0]);
                PathJSONParser parser = new PathJSONParser();
                routes = parser.parse(jObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return routes;
        }

以下所示的第128行&gt;&gt;&gt;

protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
            ArrayList<LatLng> points = null;
            PolylineOptions polyLineOptions = null;

            // traversing through routes

            for (int i = 0; i < routes.size(); i++) {
                points = new ArrayList<LatLng>();
                polyLineOptions = new PolylineOptions();
                List<HashMap<String, String>> path = routes.get(i);

Line 128            for (int j = 0; j < path.size(); j++) {
                    HashMap<String, String> point = path.get(j);

                    double lat = Double.parseDouble(point.get("lat"));
                    double lng = Double.parseDouble(point.get("lng"));
                    LatLng position = new LatLng(lat, lng);

                    points.add(position);
                }

                polyLineOptions.addAll(points);
                polyLineOptions.width(2);
                polyLineOptions.color(Color.BLUE);
            }

            googleMap.addPolyline(polyLineOptions);
        }

1 个答案:

答案 0 :(得分:0)

在此之后

List<HashMap<String, String>> path =    routes.get(i); 

添加

If(path==null) break;