Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8422751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:32:37+00:00 2026-06-10T03:32:37+00:00

I am trying to read an html file from a url source, but I

  • 0

I am trying to read an html file from a url source, but I get a nullpointerexception.
Here is my code:

public class NetworkThread extends AsyncTask<String, Integer, String> {

    URL url;
    StringBuffer sb;
    InputStream is;


    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String webUrl = etUrl.getText().toString();
        try {
            url = new URL(webUrl);
            BufferedReader in = new BufferedReader(
            new InputStreamReader(url.openStream()));
            String htmlLine;
            while ((htmlLine = in.readLine()) != null) 
                sb.append(htmlLine);

            in.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "Bad URL";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "Input / Output";
        } catch (NullPointerException e) {
            e.printStackTrace();
            return "Null";
        }
        return sb.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        html.setText(result);
    }

}

I tried running this program in regular Java – hardcoding the URL, and it runs fine.

However, when I run it on the android I get the “Null” messsage in my TextView.

LogCat say the NullPointerException is from sb.append(htmlLine);.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:32:39+00:00Added an answer on June 10, 2026 at 3:32 am

    You haven’t initialized your StringBuffer Object:

    StringBuffer sb = new StringBuffer();
    

    You can also try this code to read content from url:

    public class ReadWebpageAsyncTask extends Activity {
      private TextView textView;
    
    
    /** Called when the activity is first created. */
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.TextView01);
      }
    
      private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
          String response = "";
          for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
              HttpResponse execute = client.execute(httpGet);
              InputStream content = execute.getEntity().getContent();
    
              BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
              String s = "";
              while ((s = buffer.readLine()) != null) {
                response += s;
              }
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          return response;
        }
    
        @Override
        protected void onPostExecute(String result) {
          textView.setText(result);
        }
      }
    
      public void readWebpage(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.stackoverflow.com" });
    
      }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to read the HTML contents from a url. I tried with
I'm trying to read some text from an html file, modify it in a
I am trying to read a html file which is a plain page with
I am trying to read the post request parameters from my HTML. I can
I'm trying to launch embedded Youtube videos from a UIWebView, but get this error
Using httplib2 and urllib2, I'm trying to fetch pages from this url, but all
Are there any special considerations trying to read up data from an HTML form
i am trying to read a html file and convert it to pdf using
I'm trying to obtain data using regular expressions from a html file, by implementing
I'm trying to look at a html file and remove all the tags from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.