0

I want to calculate the apk file size of the user installed applications on phone

01-11 15:20:30.373: E/AndroidRuntime(17940):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at java.lang.Thread.run(Thread.java:1096)
01-11 15:20:30.373: E/AndroidRuntime(17940): Caused by: java.lang.NullPointerException
01-11 15:20:30.373: E/AndroidRuntime(17940):    at com.taskmanager.sb.PackageTabActivity$AppDetails.doInBackground(PackageTabActivity.java:119)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at com.taskmanager.sb.PackageTabActivity$AppDetails.doInBackground(PackageTabActivity.java:1)
01-11 15:20:30.373: E/AndroidRuntime(17940):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
 01-11 15:20:30.373: E/AndroidRuntime(17940):   at     java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-11 15:20:30.373: E/AndroidRuntime(17940):    ... 4 more

code is

       for(int i=0;i<packs.size();i++ ) {

       final PackageInfo p = packs.get(i);

       if(!isSystemPackage(p))
        {
                 String package_name = p.applicationInfo.packageName;

                 appname[i] =p.applicationInfo.loadLabel(getPackageManager()).toString();
            try
            {
                getPackageSizeInfo.invoke(pm,  package_name, new IPackageStatsObserver.Stub()
                {
                    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException
                    {

                        size[i] = pStats.codeSize/1024; // here error come says make i final


                    }});


          }catch(IllegalAccessException a) {}
            catch(IllegalArgumentException b) {}
            catch(InvocationTargetException c) {}
        }


        //    Log.v("apksize", "appsize = "+sSize+"appname = "+sName);


            }

If i make i global than i am getting NPE and if i make i final then for loop says remove final

and 119 line is that appname[i]...... How can i sove this problem?

Thanks for any help!!

Sunny
  • 14,522
  • 15
  • 84
  • 129

2 Answers2

2
01-11 15:20:30.373: E/AndroidRuntime(17940): Caused by: java.lang.NullPointerException 

01-11 15:20:30.373: E/AndroidRuntime(17940):    at com.taskmanager.sb.PackageTabActivity$AppDetails.doInBackground(PackageTabActivity.java:119)

NPE problems aren't hard to figure out. Open the file that the stack trace says has the problem, go to the line number it cites, and look at all the object references being dereferenced. One of them is null - simple as that. Step through in a debugger and you'll see it right away.

If that line with the comment is 119, I'd conclude that pstats must be null. You thought you initialized it properly, but the JVM disagrees. If not, go find line 119.

Those empty catch blocks are a very bad idea. You'll never know it if you get an exception. Always print the stack trace, like this:

try {
    // Do something in here 
} catch (Exception e) {
    e.printStackTrace();
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • i can't figure out the behaviour in the way that try to catch lines execute. The class is found here. http://stackoverflow.com/questions/8801769/find-the-apk-size-of-installed-applications pls help me to figure it out. – Sunny Jan 11 '12 at 10:59
  • I'm not sure that I understand what you're talking about. See my example above. – duffymo Jan 11 '12 at 11:26
2

If line 119 is the one that throws the NullPointerException, and line 119 is this line:

appname[i] =p.applicationInfo.loadLabel(getPackageManager()).toString();

then you'll need to handle the case where p.applicationInfo.loadLabel(getPackageManager()) returns null.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76