2017-10-19 109 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

謝謝,我幾乎是這樣做的。 –