Message
Attempt to invoke virtual method 'int java.lang.String.length ()' on a null object reference
Call
JSONArray data = new JSONArray(getHttpGet(url));
Code:
public static String getHttpGet(String url) {
String result = null;
try {
URL myurl = new URL("http://www.exemplo.com.br/gerajson.php");
HttpURLConnection urlconnection = (HttpURLConnection) myurl.openConnection();
urlconnection.setRequestMethod("GET");
urlconnection.setDoInput(true);
urlconnection.connect();
InputStream is = urlconnection.getInputStream();
if (is != null) {
StringBuffer sb = new StringBuffer();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} finally {
is.close();
}
result = sb.toString();
}
} catch (Exception e) {
result = null;
}
return result;
}
Update (when changing contents of catch
):
app has stopped. E.AndroidRuntime: FATAL EXCEPTION: mainProcess: br.com.webvisionsystems.shishamaps, PID: 2561 java.lang.RuntimeException: android.os.NetworkOnMainThreadException at br.com.webvisionsystems.shishamaps.MapFragment.getHttpGet (MapFragment.java:17 6) at br.com.webvisionsystems.shishamaps.MapaFragment.onMapReady (MapFragment.java:64)