0

I am attempting to implement DownloadManager in my current Android project to download an mp3 from a URL. The Android SDK version that I'm using is 2.3.3. The download is started by a button click even. From there a string is passed to the downloadPodcast method to begin the download. The problem I've ran into is I am getting a null pointer exception when attempting to start the download. I have ran through debugger and verified that the URL string is properly passed to this method and contains a valid string value. The specific line that is failing is:

downloadId= dm.enqueue(new DownloadManager.Request(Uri.parse(podcastUrl))

After debugging I believe I have narrowed it down to 'dm'. The value of this item is always "null" even after, what I believe is, properly initializing it in the onCreate method. I've looked through several examples and believe I am implementing this properly, I'm just at a dead end and not sure why this is still null.

Main Activity code:

public class Podcasts extends ListActivity{

private DownloadManager dm;
private long downloadId=-1L;

@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.podcasts); 

    dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

Download method:

public void downloadPodcast (String podcastUrl){


    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdir();


    downloadId= dm.enqueue(new DownloadManager.Request(Uri.parse(podcastUrl))
        .setAllowedOverRoaming(false)
        .setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI)
        .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.mp3")
        .setTitle("TEST"));


}

Added Logcat:

FATAL EXCEPTION: main
java.lang.NullPointerException
at tv.undignified.android.Podcasts.downloadPodcast(Podcasts.java:275)
at tv.undignified.android.Podcasts$CustomAdapter$2.onClick(Podcasts.java:218)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Fostenator
  • 83
  • 2
  • 12
  • Are you sure it's `dm` that's null? Can you post the LogCat to verify this? – Cat Aug 09 '12 at 03:36
  • Thanks for looking at it. I've added the LogCat to the description above. Also, if I perform a debug and examine the line I mentioned above, the only item that is null is 'dm'. – Fostenator Aug 09 '12 at 03:45
  • 1
    I agree with @Eric `dm can't be null. You add proper permissions in your `manifest` file? i.e. `INTERNET,ACCESS_NETWORK_STATE,WRITE_EXTERNAL_STORAGE` – Mohsin Naeem Aug 09 '12 at 04:02
  • Do you modify `dm` anywhere else? Also try checking the value of `dm` using something like `android.util.Log.i("TEST", dm+"");` at the end of `onCreate`, `onClick`, and through your other methods as well. – Cat Aug 09 '12 at 04:34
  • No, it's not modified anywhere else. According to what I've read I shouldn't have to use it anywhere else. – Fostenator Aug 09 '12 at 10:34
  • I implemented `android.util.Log.i("TEST", dm+"");` and verified that dm is logging as null in LogCat. – Fostenator Aug 09 '12 at 13:14

1 Answers1

3

Figured the problem out. I was initializing DownloadManager in my onCreate for my Activity, but attempting to access it through a class within this Activity that was creating a new instance of my main activity, one where DownloadManager was never initialized.

To resolve the issue I created a setter and getter method for my download URL and passed it back to my main activity from the class within my activity and then called the method that starts the download manager.

Fostenator
  • 83
  • 2
  • 12