-2

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)
Hans Olo
  • 9
  • 3

2 Answers2

1

you are returning null in finally block. move return null; to outside finally block

Omer
  • 650
  • 5
  • 15
0

Finally is the block that will always be executed(unless system.exit() will be done). Thats why it is always null.

Tip:- always use a " " in log's second argument, otherwise it will throw exception in case of null, like.. message should not be null etc..

Log.e("String", " "+result);
SRB Bans
  • 3,096
  • 1
  • 10
  • 21