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

  • Home
  • SEARCH
  • 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 8989687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:19:22+00:00 2026-06-15T22:19:22+00:00

As I was thinking that downloading is pretty straightforward I am encountering a error

  • 0

As I was thinking that downloading is pretty straightforward I am encountering a error when I try to download an xml file from the internet and save it on the internal memory. I use the internal memory because I am testing my app on samsung galaxy s3.

Here’s the code for the downloader. It’s actually a thread which is invoked from the activity

public class DownloaderThread extends Thread
{
    private static final int DOWNLOAD_BUFFER_SIZE = 4096;
    Context context;
    private String downloadUrl = "some address";

    public void run()
    {
            URL url;
            URLConnection conn;
            String fileName;
            BufferedInputStream inStream;
            BufferedOutputStream outStream;
            File outFile;
            FileOutputStream fileStream;

            try
            {
                    url = new URL(downloadUrl);
                    conn = url.openConnection();
                    conn.setUseCaches(false);
                    fileName = "file.xml";

                    Log.v("XML", "Download Started");

                    // start download
                    inStream = new BufferedInputStream(conn.getInputStream());
                    outFile = new File(context.getFilesDir() + "/" + fileName);
                    fileStream = new FileOutputStream(outFile);
                    outStream = new BufferedOutputStream(fileStream, DOWNLOAD_BUFFER_SIZE);
                    byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
                    int bytesRead = 0;
                    while(!isInterrupted() && (bytesRead = inStream.read(data, 0, data.length)) >= 0)
                    {
                            outStream.write(data, 0, bytesRead);
                    }

                    outStream.close();
                    fileStream.close();
                    inStream.close();

                    if(isInterrupted())
                    {
                            outFile.delete();
                    }
                    else
                    {
                            Log.v("XML","Download Finished");
                            Log.v("PATH","File is stored in direcotry:" + outFile.getAbsolutePath().toString());
                    }
            }
            catch(MalformedURLException e)
            {
                e.printStackTrace();
                Log.v("Error",e.toString());

            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
                Log.v("Error",e.toString());
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Log.v("Error",e.toString());
            }
    }

and here is the code which invokes the thread :

public boolean onOptionsItemSelected(MenuItem item) {
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
     android.net.NetworkInfo datac = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
     if ( item.getItemId() == DOWNLOAD_XML_MENU_ITEM ) {
            if ((wifi != null & datac != null) && (wifi.isConnected() | datac.isConnected())) 
            {
                downloaderThread = new DownloaderThread();
                downloaderThread.start();
            }
            else 
            {
                displayConnectionAlert();
            }
        }
        return true;
    }

the errors I receive are:
tag trace – no such file and directory
and after log downloaded started i get NullPointerException on the path of the outFile. Any ideas why?

  • 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-15T22:19:23+00:00Added an answer on June 15, 2026 at 10:19 pm

    You’re using a Context reference that is never assigned a value, hence the NPE.

    Context context; // never initialised anywhere, thus null
    

    Since all the variable is used for is to resolve context.getFilesDir(), I suggest you simply create a constructor for your thread class that takes the path as an argument; i.e.:

    public DownloaderThread(String targetDir) { /* implement here */ }
    

    Alternatively you can actually pass in a Context instance, but just make sure it’s the application context (rather than an Activity) to avoid potential memory leaks.

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

Sidebar

Related Questions

Thinking that the answer to this is pretty obvious but here it goes: When
I need help with downloading from webserver... What i currently do is get XML
I have an app that is downloading a zip file and then copying this
I want to download songs and photos with my iPhone from a website that
Is it possible to load activities from an external dex-file? I'm thinking of some
I have a webapp that needs to sometimes download some bytes from a url
We have a ftp system setup to monitor/download from remote ftp servers that are
I was thinking that I would have two tables for mysql. One for storing
Am I right in thinking that until I am able to afford dedicated servers
I was thinking that the SQLDataReader should not work if there is no connection

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.