ProgressDialog setMax throws NullPointerException
I am unwell and stuck at home so I decided to dive into my Android app (a new one this time!). I wanted to perform some tasks that could be long running (doubtfully but why freeze the UI thread ever). I want to show a ProgressDialog here but it doesn’t work with the static factory method, ProgressDialog.show(…). I keep getting the same exception again and again.
07-20 22:57:16.445: ERROR/AndroidRuntime(25843): FATAL EXCEPTION: main 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): java.lang.NullPointerException 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.app.ProgressDialog.onProgressChanged(ProgressDialog.java:318) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.app.ProgressDialog.setMax(ProgressDialog.java:233) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at com.codercorp.android.playlist.ExporterTask.onPreExecute(ExporterTask.java:68) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.os.AsyncTask.execute(AsyncTask.java:391) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at com.codercorp.android.playlist.PlaylistExporterMain.onClick(PlaylistExporterMain.java:81) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.view.View.performClick(View.java:2485) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.view.View$PerformClick.run(View.java:9080) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.os.Handler.handleCallback(Handler.java:587) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.os.Handler.dispatchMessage(Handler.java:92) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.os.Looper.loop(Looper.java:130) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at android.app.ActivityThread.main(ActivityThread.java:3683) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at java.lang.reflect.Method.invokeNative(Native Method) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at java.lang.reflect.Method.invoke(Method.java:507) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 07-20 22:57:16.445: ERROR/AndroidRuntime(25843): at dalvik.system.NativeStart.main(Native Method) 07-20 23:02:38.425: ERROR/jdwp(25924): Failed sending reply to debugger: Broken pipe
After googling for a good amount of time I came across this bug report in the Android project http://code.google.com/p/android/issues/detail?id=3114. The report’s been up for over two years now but Google hasn’t bothered to fix it (shame!). As is mentioned in the report, the problem is with the update handler variable not being set before the dialog box is shown. The solution is simple, create your own ProgressDialog without using the static factory method.
ProgressDialog progressDialog = new ProgressDialog(PlaylistExporterMain.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setCancelable(false); progressDialog.setIndeterminate(false); progressDialog.setMax(getListAdapter().getCount()); progressDialog.show();
If you have run across this bug, please go to the bug report and leave a comment, maybe it’ll force Google’s hand into fixing the bug (which requires an addition to the ProgressDialog static factory methods).









Thanks for posting this solution. I’ve been hunting this error down for two days now.
This does not appear to be a bug. It appears to be erroneous use of setting the style after the dialog is created. Apparently you’re not allowed to do that. I had the same problem, if I create the dialog as described above, and set the style at a later time. So, the fact of the matter is, if you want to change the style, you need to create a dialog yourself, as described above, and set the style BEFORE calling the show() method.
This makes perfect sense, in that the initialisation of appropriate dialog setup would happen on the request to “show” it. The only thing that doesn’t make sense, is that google didn’t document it in the show() method.
Apologies for the late reply, just saw this. I agree that its not really a bug but the fact that its not documented just hurts sooooo much. The lack of documentation is what made me think that this was a bug.