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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:44:25+00:00 2026-05-24T04:44:25+00:00

I’m trying to export a CSV file to the SD card with an AsyncTask

  • 0

I’m trying to export a CSV file to the SD card with an AsyncTask and using a ProgressBar to tell the user how that task is running. However, when I attempt to set the progress bar with setProgress I get a null pointer exception. My Logcat shows the integer I’m passing as 8, so that isn’t null. If I comment out my attempt to set the progress bar it runs correctly. What is the runtime seeing as null when I try to set the progress bar?

The full AsyncTask implementation:

public class Exporter extends AsyncTask<File, Integer, Boolean> {
    private Context ctx;
    private final static String TAG = "Exporter";

    private ProgressDialog progress;

    private GeoDatabase db;
    private RecordListActivity parent;

    public Exporter(Context ctx) {
        super();
        Log.d(TAG, "Building " + this + " from " + ctx);
        this.ctx = ctx;
        parent = (RecordListActivity) ctx;
        db = (GeoDatabase) ctx.getApplicationContext();
    }

    @Override
    protected void onPreExecute() {
        // Initialize the progress dialog
        Log.i(TAG, "Pre-executing exporter");
        final String title = "Exporting";
        final String msg = "Initializing export...";
        progress = ProgressDialog.show(ctx, title, msg, true);
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        super.onPreExecute();
    }

    /**
     * Begins operation of the exporter and determines if it was a
     * success.
     * 
     * @param args the destination file
     * 
     * @return whether the transfer was successful
     */
    @Override
    protected Boolean doInBackground(final File... args) {
        Log.d(TAG, "src-> " + "N/A" + ", dest-> " + args[0]);

        // Get the ArrayList of records inside the current folder
        ArrayList<Record> records = db.getRecordsIn(parent.getCurrentFolder());

        FileWriter writer;
        try {
            // Build a FileWriter targeting the destination
            writer = new FileWriter(args[0]);

            // Iterate through the records to write their CSV string out
            final int total = records.size();
            int current = 1;
            for (Record record : records) {
                Log.v(TAG, "Accessing " + record);
                writer.write(record.getCSVString() + "\n");
                Log.v(TAG, record.getCSVString());

                // Update the progress bar
                onProgressUpdate(total, current);
                current++;
            }

            // Close the writer
            writer.close();
            Log.d(TAG, "Writer closed");
        } catch (IOException e) {
            // Something has gone horribly wrong
            e.printStackTrace();
            Log.e(TAG, "Writing to file failed!");
            return false;
        }

        return true;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
            // values[1] is the current record being written
        // values[0] is the total number of records being written
        final int total = values[0];
        final int current = values[1];

        // Updates the text for the current record
        final String msg = "Writing record " + current + " of " + total;
        progress.setMessage(msg);
        Log.d(TAG, msg);

        // Updates the progress bar
        final int position = 100 * current / total;
        Log.d(TAG, "Setting position to " + position);
        progress.setProgress(position);
    }

    // can use UI thread here
    protected void onPostExecute(final Boolean success) {
        // TODO: Alert the user to success or failure - the dialog drops
        // to quickly now
        if (success) {
            progress.setMessage("Export successful");
            Log.i(TAG, "Export successful");
        } else {
            progress.setMessage("Export failed");
            Log.e(TAG, "Export failed");
        }
        progress.dismiss();
    }
}

The full relevant Logcat:

 24236     RecordListActivity  D  Export button pressed
 24236     RecordListActivity  D  Calling exporter to write out data
 24236     RecordListActivity  D  Building the dialog for exporting
 24236     RecordListActivity  D  OK clicked on export dialog
 24236               Exporter  D  Building edu.lafayette.cs.geology.Exporter@408dc328 from edu.lafayette.cs.geology.RecordListActivity@407ee0a0
 24236               Exporter  I  Pre-executing exporter
 24236               Exporter  D  src-> N/A, dest-> /mnt/sdcard/data.csv
 24236               Exporter  V  Accessing edu.lafayette.cs.geology.GeoDatabase$Bedding@408ea330
 24236               Exporter  V  1, 1311358183278,  latitude for bedding,  longitude for bedding, strike, dip, observed  a bedding , dip-direct
 24236               Exporter  D  Writing record 1 of 12
 24236               Exporter  D  Setting position to 8
 24236               dalvikvm  W  threadid=9: thread exiting with uncaught exception (group=0x401ab760)
 24236         AndroidRuntime  E  FATAL EXCEPTION: AsyncTask #1
 24236         AndroidRuntime  E  java.lang.RuntimeException: An error occured while executing doInBackground()
 24236         AndroidRuntime  E    at android.os.AsyncTask$3.done(AsyncTask.java:266)
 24236         AndroidRuntime  E    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 24236         AndroidRuntime  E    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 24236         AndroidRuntime  E    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 24236         AndroidRuntime  E    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 24236         AndroidRuntime  E    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
 24236         AndroidRuntime  E    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
 24236         AndroidRuntime  E    at java.lang.Thread.run(Thread.java:1020)
 24236         AndroidRuntime  E  Caused by: java.lang.NullPointerException
 24236         AndroidRuntime  E    at android.app.ProgressDialog.onProgressChanged(ProgressDialog.java:346)
 24236         AndroidRuntime  E    at android.app.ProgressDialog.setProgress(ProgressDialog.java:207)
 24236         AndroidRuntime  E    at edu.lafayette.cs.geology.Exporter.onProgressUpdate(Exporter.java:114)
 24236         AndroidRuntime  E    at edu.lafayette.cs.geology.Exporter.doInBackground(Exporter.java:86)
 24236         AndroidRuntime  E    at edu.lafayette.cs.geology.Exporter.doInBackground(Exporter.java:1)
 24236         AndroidRuntime  E    at android.os.AsyncTask$2.call(AsyncTask.java:252)
 24236         AndroidRuntime  E    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 24236         AndroidRuntime  E    ... 4 more
  • 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-05-24T04:44:25+00:00Added an answer on May 24, 2026 at 4:44 am

    Found the problem and a workaround here. It appears to be a known bug in the Android API where the factory method for creating a ProgressDialog with ProgressDialog.show(params) doesn’t properly set the update handler. So the revised, working code is as follows:

    @Override
    protected void onPreExecute() {
        // Initialize the progress dialog
        Log.i(TAG, "Pre-executing exporter");
        final String title = "Exporting";
        final String msg = "Initializing export...";
        progress = new ProgressDialog(ctx);
        progress.setTitle(title);
        progress.setMessage(msg);       
        progress.setIndeterminate(false);
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        super.onPreExecute();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.