0

My app is working fine wheni debug and run but when i make its Apk, and open Apk File, it is not opening and giving me this error.

Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[])' on a null object reference

My code is

private class TaskScan extends AsyncTask<Void, Integer, List<AppsListItem>> {

    private int mAppCount = 0;

    @Override
    protected void onPreExecute() {
        if (mOnActionListener != null) {
            mOnActionListener.onScanStarted(CleanerService.this);
        }
    }

    @Override
    protected List<AppsListItem> doInBackground(Void... params) {
        mCacheSize = 0;

        final List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(
                PackageManager.GET_META_DATA);

        publishProgress(0, packages.size());

        final CountDownLatch countDownLatch = new CountDownLatch(packages.size());

        final List<AppsListItem> apps = new ArrayList<>();

        try {
            for (ApplicationInfo pkg : packages) {
                mGetPackageSizeInfoMethod.invoke(getPackageManager(), pkg.packageName,
                        new IPackageStatsObserver.Stub() {

                            @Override
                            public void onGetStatsCompleted(PackageStats pStats,
                                                            boolean succeeded)
                                    throws RemoteException {
                                synchronized (apps) {
                                    publishProgress(++mAppCount, packages.size());

                                    mCacheSize += addPackage(apps, pStats, succeeded);
                                }

                                synchronized (countDownLatch) {
                                    countDownLatch.countDown();
                                }
                            }
                        }
                );
            }

            countDownLatch.await();
        } catch (InvocationTargetException | InterruptedException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return new ArrayList<>(apps);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        if (mOnActionListener != null) {
            mOnActionListener.onScanProgressUpdated(CleanerService.this, values[0], values[1]);
        }
    }

    @Override
    protected void onPostExecute(List<AppsListItem> result) {
        if (mOnActionListener != null) {
            mOnActionListener.onScanCompleted(CleanerService.this, result);
        }

        mIsScanning = false;
    }

    private long addPackage(List<AppsListItem> apps, PackageStats pStats, boolean succeeded) {
        long cacheSize = pStats.cacheSize;

        //if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            cacheSize += pStats.externalCacheSize;
        //}

        if (!succeeded || cacheSize <= 0) {
            return 0;
        }

        try {
            PackageManager packageManager = getPackageManager();
            ApplicationInfo info = packageManager.getApplicationInfo(pStats.packageName,
                    PackageManager.GET_META_DATA);

            apps.add(new AppsListItem(pStats.packageName,
                    packageManager.getApplicationLabel(info).toString(),
                    packageManager.getApplicationIcon(pStats.packageName),
                    cacheSize));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return cacheSize;
    }
}

and the other class is

    private class TaskClean extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        if (mOnActionListener != null) {
            mOnActionListener.onCleanStarted(CleanerService.this);
        }
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);

        StatFs stat = new StatFs(Environment.getDataDirectory().getAbsolutePath());

        try {
            if (canCleanInternalCache(CleanerService.this)) {
                mFreeStorageAndNotifyMethod.invoke(getPackageManager(),
                        (long) stat.getBlockCount() * (long) stat.getBlockSize(),
                        new IPackageDataObserver.Stub() {
                            @Override
                            public void onRemoveCompleted(String packageName, boolean succeeded)
                                    throws RemoteException {
                                countDownLatch.countDown();
                            }
                        }
                );
            } else {
                countDownLatch.countDown();
            }


                if (isExternalStorageWritable()) {
                    final File externalDataDirectory = new File(Environment
                            .getExternalStorageDirectory().getAbsolutePath() + "/Android/data");

                    final String externalCachePath = externalDataDirectory.getAbsolutePath() +
                            "/%s/cache";

                    if (externalDataDirectory.isDirectory()) {
                        final File[] files = externalDataDirectory.listFiles();

                        for (File file : files) {
                            if (!deleteDirectory(new File(String.format(externalCachePath,
                                    file.getName())), true)) {
                                Log.e(TAG, "External storage suddenly becomes unavailable");

                                return false;
                            }
                        }
                    } else {
                        Log.e(TAG, "External data directory is not a directory!");
                    }
                } else {
                    Log.d(TAG, "External storage is unavailable");
                }


            countDownLatch.await();
        } catch (InterruptedException | IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            return false;
        }

        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            mCacheSize = 0;
        }

        if (mOnActionListener != null) {
            mOnActionListener.onCleanCompleted(CleanerService.this, result);
        }

        mIsCleaning = false;
    }

    private boolean deleteDirectory(File file, boolean directoryOnly) {
        if (!isExternalStorageWritable()) {
            return false;
        }

        if (file == null || !file.exists() || (directoryOnly && !file.isDirectory())) {
            return true;
        }

        if (file.isDirectory()) {
            final File[] children = file.listFiles();

            if (children != null) {
                for (File child : children) {
                    if (!deleteDirectory(child, false)) {
                        return false;
                    }
                }
            }
        }
        file.delete();

        return true;
    }

    private boolean isExternalStorageWritable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
}

// Code where i am using these classes

public void cleanCache() {
    mIsCleaning = true;
    try{
        new TaskClean().execute();
    }
    catch (NullPointerException e){
        e.printStackTrace();
    }

}
laraib
  • 43
  • 7
  • Something is null somewhere when it's being accessed. Read the logcat output, determine the line where it's failing, and you can trace from there. – terencey Nov 08 '17 at 05:48
  • @squeeish i got no error when i simply debug and run it, but it is giving me error when i make its Apk File.. So how can i trace it? – laraib Nov 08 '17 at 05:53
  • In logcat, you can find the line where the error is happening when you run the apk file on your device, regardless of whether you ran a signed apk or a debug apk. – terencey Nov 08 '17 at 06:01
  • It is not specifying any line in Logcat – laraib Nov 08 '17 at 06:53

1 Answers1

0

Check that you attached EXTERNAL_STORAGE (READ/WRITE) permissions in the manifest, check for possible Null Cases which may appear and also verify that the TaskScan and TaskScan are dependent to each other or not

LordGrim
  • 70
  • 5