-1

I am a newbie programmer in Android, I am Stuck Here, Achieving this, Wanted to Print Out Url Contents In an EditText With this Chunk.

private class MyTask extends AsyncTask<String, Void, String> {
    Activity activity;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {
        String url = strings[0];

        try {
            StringBuilder sb = new StringBuilder();
            URL link = new URL(url);
            BufferedReader in;
            in = new BufferedReader(new InputStreamReader(link.openStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                sb.append(inputLine);
            in.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
@Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        EditText editText = activity.findViewById(R.id.edittext);
        editText.setText(s);
    }

And Main Activity (onCreate): as Follows

        new MainActivity.MyTask().execute("http://yahoo.com");

And here is my fancy looking Error :/

06-20 19:09:08.466 31168-31175/? E/art: Failed sending reply to debugger: Broken pipe 06-20 19:09:09.563 31168-31168/com.curiosityworks.www.weatherlite E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.curiosityworks.www.weatherlite, PID: 31168
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference at com.curiosityworks.www.weatherlite.MainActivity$MyTask.onPostExecute(MainActivity.java:102) at com.curiosityworks.www.weatherlite.MainActivity$MyTask.onPostExecute(MainActivity.java:70) at android.os.AsyncTask.finish(AsyncTask.java:667) at android.os.AsyncTask.-wrap1(AsyncTask.java) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6377) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

Please Guide me, I am Wasting a lot of precious time to figure it out, it will be a big help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rahul Singh
  • 243
  • 3
  • 15

2 Answers2

0

You have a field

Activity activity;

which is null because you never assign it.

You try to access it in onPostExecute, this results in a null pointer. In other words, the Activity does not exist in the onPostExecute.

I do not know what the rest of your code looks like so I can't give you new code, but I suggest reading how AsyncTasks work (or even better, use RxJava, here is an example anyway http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html)

Zun
  • 1,553
  • 3
  • 15
  • 26
-1

public class MainActivity extends Activity {

public class DownloadTask extends AsyncTask<String, Void, String> {

    @Override
    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 += current;

                data = reader.read();

            }

            return result;

        }
        catch(Exception e) {

            e.printStackTrace();

            return "Failed";

        }


    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DownloadTask task = new DownloadTask();
    String result = null;

    try {

        result = task.execute("//website").get();
        editText.setText(result);


    } catch (InterruptedException e) {

        e.printStackTrace();

    } catch (ExecutionException e) {

        e.printStackTrace();

    }

    Log.i("Contents Of URL", result);

}