2017-10-19 112 views
1

我努力从MySQL数据库接收谷歌地图标记。 我有这个问题java.lang.NullPointerException:尝试调用空对象引用上的虚拟方法'int java.util.ArrayList.size()'。我的代码如何自动实现地图标记?错误的连接或错误的代码?

部分:

ArrayList<HashMap<String, String>> location = null; 
    try { 

     JSONArray data = new JSONArray(RetrieveTask.class); 

     location = new ArrayList<HashMap<String, String>>(); 
     HashMap<String, String> map; 

     for(int i = 0; i < data.length(); i++) { 
      JSONObject c = data.getJSONObject(i); 

      map = new HashMap<String, String>(); 
      map.put("id", c.getString("id")); 
      map.put("lat", c.getString("lat")); 
      map.put("lng", c.getString("lng")); 
      location.add(map); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    for (int i = 0; i < location.size/*here it shows the problem - size*/(); i++) { 
     String id = location.get(i).get("id"); 
     double lat = Double.parseDouble(location.get(i).get("lat")); 
     double lng = Double.parseDouble(location.get(i).get("lng")); 
     LatLng latLng = new LatLng(lat, lng); 
     MarkerOptions marker = new MarkerOptions().position(latLng).title(id); 
     googleMap.addMarker(marker); 
    } 

而且从连接到interenet代码:

private class RetrieveTask extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... voids) { 
     String strUrl = "http://12345.php"; 
     URL url = null; 
     StringBuffer sb = new StringBuffer(); 

     try { 
      url = new URL(strUrl); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      InputStream iStream = connection.getInputStream(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(iStream)); 
      String line = ""; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line); 
      } 

      reader.close(); 
      iStream.close(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return sb.toString(); 
    } 

} 

我不知道什么是错我的代码,我已经尝试了不同的方式,但仍然得到了同样的错误。 以前有没有人遇到同样的问题,你是怎么理清的?

可能是数据库故障,因为它不是远程的,我使用本地主机? JSON响应正常工作,它返回期望值。

回答

0

为确保您有NPE,因为你赶上JSONException

catch (JSONException e) { 
     e.printStackTrace(); 
} 

而你得到了它,因为这条线

JSONArray data = new JSONArray(RetrieveTask.class); 

你是不是从互联网上这条线获取数据。您只需尝试从class对象创建JSONArray

你必须在你的AsyncTask

protected void onPostExecute(String result) { 
    //call here method of Activity or parse your JSONArray 
    JSONArray data = new JSONArray(result); 
    //your other code 
} 
+0

谢谢,只是改变了我的代码,现在它完美的工作。 –

0

使用方法onPostExecute我想你不遵循正确的步骤从本地主机检索数据。您首先需要覆盖onPostExecute() AsyncTask的方法,然后您应该从onPostExecute()方法中检索localhost中的结果数据。

你的代码崩溃,在这条线

JSONArray data = new JSONArray(RetrieveTask.class); 

,因此地点尚未初始化。所以当你尝试访问下一个for循环的位置时,它会抛出一个NullPointerException异常。

这是你的代码应该是什么样子:

private class RetrieveTask extends AsyncTask<Void, Void, String> { 

@Override 
protected String doInBackground(Void... voids) { 
    String strUrl = "http://12345.php"; 
    URL url = null; 
    StringBuffer sb = new StringBuffer(); 

    try { 
     url = new URL(strUrl); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.connect(); 
     InputStream iStream = connection.getInputStream(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(iStream)); 
     String line = ""; 

     while ((line = reader.readLine()) != null) { 
      sb.append(line); 
     } 

     reader.close(); 
     iStream.close(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return sb.toString(); 
} 

@Override 
protected String onPostExecute(String result){ 
ArrayList<HashMap<String, String>> location = null; 
try { 

    JSONArray data = new JSONArray(result); 

    location = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> map; 

    for(int i = 0; i < data.length(); i++) { 
     JSONObject c = data.getJSONObject(i); 

     map = new HashMap<String, String>(); 
     map.put("id", c.getString("id")); 
     map.put("lat", c.getString("lat")); 
     map.put("lng", c.getString("lng")); 
     location.add(map); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

//the if loop is to check if location is initialised above 
if(location!=null){ 
     for (int i = 0; i < location.size/*here it shows the problem - size*/(); i++) { 
      String id = location.get(i).get("id"); 
      double lat = Double.parseDouble(location.get(i).get("lat")); 
      double lng = Double.parseDouble(location.get(i).get("lng")); 
      LatLng latLng = new LatLng(lat, lng); 
      MarkerOptions marker = new MarkerOptions().position(latLng).title(id); 
      googleMap.addMarker(marker); 
     } 
    } 
} 

}

然后,你可以用这种方式执行的AsyncTask:

new RetrieveTask().execute(); 

这里的AsyncTask的一个例子,它会帮助你理解相同的流程。

You can skip to Step no. 7 to know about the Android setup for connecting to localhost

+0

谢谢,我几乎是这样做的。 –