0

I am downloading the content from URL. But it is giving error NullPointerException : println needs a message. I have added URL and the html content of the website I want to print in Log.

 protected String doInBackground(String... urls) {

    String result = "";
    URL url;
    HttpURLConnection urlConnection = null;

    try{
        url = new URL(urls[0]);
        urlConnection = (HttpURLConnection)url.openConnection();
        InputStream in = urlConnection.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        int data = reader.read();

        while(data!=-1){
            char current = (char) data;
            result += result;
            data = reader.read();
        }

    }catch (Exception e){
        e.printStackTrace();
    }

    return null;
   }

The error:

2019-04-28 17:36:44.355 15741-15741/com.example.guessthecelebraty E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.guessthecelebraty, PID: 15741
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guessthecelebraty/com.example.guessthecelebraty.MainActivity}: java.lang.NullPointerException: println needs a message
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.i(Log.java:166)
        at com.example.guessthecelebraty.MainActivity.onCreate(MainActivity.java:65)

I want to connect it to the website and download the content into the Log.

TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37
Omkar Waghe
  • 169
  • 1
  • 10
  • i dont see log anywhere in the code. pls update your question with more information. may be add complete code. – madhu s Apr 28 '19 at 12:26
  • Your stack trace is not coming from this code. It is coming from line 65 of `MainActivity.java`, in your `onCreate()` method (`com.example.guessthecelebraty.MainActivity.onCreate(MainActivity.java:65)`). – CommonsWare Apr 28 '19 at 12:29
  • share full code – Radesh Apr 28 '19 at 12:31

1 Answers1

0

Log statement needs a message to print. Since you are performing a async task try to use Log statement in postExecute, provide a null check to avoid this exception.

if (message != null) {
    Log.d("message", message);
} else {
    Log.e("message", "empty");
}
Sasi Kanth
  • 709
  • 8
  • 17