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 8202751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:18:03+00:00 2026-06-07T07:18:03+00:00

I have Implemented Code to download a File from Internet, But I am getting

  • 0

I have Implemented Code to download a File from Internet, But I am getting Null Pointer Exception while updating my GUI thread with progress value(progress percentage).

Here Is my Code.

 public class CustomviewActivity extends Activity {
ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


 // instantiate it within the onCreate method
 mProgressDialog = new ProgressDialog(CustomviewActivity.this);
 mProgressDialog.setMessage("File Downloading Start....");
 mProgressDialog.setIndeterminate(false);
 mProgressDialog.setMax(100);
 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

 // execute this when the downloader must be fired
 mProgressDialog.show();
 Asynctask downloadFile = new Asynctask();
 downloadFile.execute("http://sound21.mp3pk.com/indian/jodibreakers/jodi-breakers03(www.songs.pk).mp3");
}

Here is MY Asynctask………

public class Asynctask extends AsyncTask<String, Integer, String> {
       @Override
        protected String doInBackground(String... sUrl) {
            try {
                URL url = new URL(sUrl[0]);
                URLConnection connection = url.openConnection();
                connection.connect();
                // this will be useful so that you can show a typical 0-100% progress bar
                int fileLength = connection.getContentLength();

                // download the file
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/file_name.mp3");

                byte data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {

            }
            return null;
       }

@Override
protected void onProgressUpdate(Integer... values) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(values);
     mProgressDialog.setProgress(values[0]);
}
@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}

}

Following Error I am getting in LogCat

07-07 22:30:38.496: ERROR/AndroidRuntime(22625): FATAL EXCEPTION: main
07-07 22:30:38.496: ERROR/AndroidRuntime(22625): java.lang.NullPointerException
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.custom.CustomviewActivity$Asynctask.onProgressUpdate(CustomviewActivity.java:103)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.custom.CustomviewActivity$Asynctask.onProgressUpdate(CustomviewActivity.java:1)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.os.Looper.loop(Looper.java:150)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.app.ActivityThread.main(ActivityThread.java:4389)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at java.lang.reflect.Method.invoke(Method.java:507)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at dalvik.system.NativeStart.main(Native Method)
  • 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-07T07:18:05+00:00Added an answer on June 7, 2026 at 7:18 am

    You should begin showing the ProgressDialog in onPreExecute() instead. See this post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have implemented code like here but it's not working for iOS 5.0, it
I want to download a file from Internet and InternetReadFile seem a good and
I have a download action implemented on my Vaadin application but for some reason
I have been trying to follow this example (download the source code from a
I have implemented a basic file upload/download on top of AppEngine datastore. The functionality
I have implemented the code in Devise call backs in my application controller. First
FOR ANYONE INTERESTED: I have implemented the code for the behaviour I am looking
I have this code implemented and I like how straight forward it is because
I have implemented the image upload code using jquery php in my php web
I have implemented correctly bump's api, and added this code: - (void) configureBump {

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.