I have a very basic program in Android Studio that gets a Json-Object from a website ("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt") and the just displays it on screen, without parsing it. The Json-object is transmitted correctly, but at onPostExecute the String result seems to be null and the Log.e("setText") is throwing a NullPointerException, while Log.e("String") is correct and neither the IOException nor the MalformedURLException is thrown:
try {
url = new URL("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
Log.e("TRY", "trybracket");
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String result = buffer.toString();
Log.e("String", result);
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e("MUE", "Malformed");
} catch (IOException e) {
e.printStackTrace();
Log.e("IOE", "IOException");
} finally {
if (connection != null) {
connection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvData.setText(result);
Log.e("setText", result);
}
}
Also this my Logcat:
E/TRY: trybracket
E/String: { "movies": [{"movie": "Avengers","year": 2012}]}
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process com.stasiak.jsonparsing, PID: 20061
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java:238)
at com.stasiak.jsonparsing.MainActivity$JSONTask.onPostExecute(MainActivity.java:96)
at com.stasiak.jsonparsing.MainActivity$JSONTask.onPostExecute(MainActivity.java:39)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)<1 internal calls>
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)