You're getting the nullPointerException
because you're expecting a JSON
response, but since you have no network, there is no response to parse.
One way to prevent this code from running is to simply place a null check before you go into the loop:
if(json != null){
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String parsed_results_formatted_address = c
.getString("formatted_address");
arrfortextviews.add(parsed_results_formatted_address);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
If it were me, I'd check for network connectivity before executing the code. This could be done before you call the execute of the AsyncTask
:
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//if there is a connection
if (netInfo != null && netInfo.isConnectedOrConnecting()){
...put your code here...
}
Ultimately it just depends on whether you need the AsycTask
to run if there is no connection.