1
Message
Attempt to invoke virtual method 'int java.lang.String.length()' on a null Object Reference
Calling for
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 the content of catch
):
app has stopped. br.com.webvisionsistemas.shishamaps E/AndroidRuntime: FATAL EXCEPTION: mainProcess: br.com.webvisionsistemas.shishamaps, PID: 2561 java.lang.RuntimeException: android.os.NetworkOnMainThreadException at br.com.webvisionsistemas.shishamaps.MapaFragment.getHttpGet( Mapafragment.java:17 6) at br.com.webvisionsistemas.shishamaps.Mapafragment.onMapReady( Mapafragment.java:64)
} catch (Exception e) { result = null; }
- This from here is not a good idea, you’re swallowing the real mistake to mask it like aNullPointerException
after. If you change thisresult = null;
by ae.printStackTrace(); throw new RuntimeException(e);
, what appears?– Victor Stafusa
app has stopped. br.com.webvisionsistemas.shishamaps E/Androidruntime: FATAL EXCEPTION: mainProcess: br.com.webvisionsistemas.shishamaps, PID: 2561 java.lang.Runtimeexception: android.os.Networkonmainthreadexception at br.com.webvisionsistemas.shishamaps.Mapafragment.getHttpGet(Mapafragment.java:176) at br.com.webvisionsistemas.shishamaps.Mapafragment.onMapReady(Mapafragment.java:64)
– Tiago Pelais