Working on clear cache App getting Error in an Asynctask method












0














I am working on file scanner App and clear cache it is crash on activity . my application has clear cache like boost application, unstall apk from app Ram boost etc ........



 private void travelPath(File root, int level) {
if (root == null || !root.exists() || level > SCAN_LEVEL) {
return;
}

File lists = root.listFiles();
for (File file : lists) {
if (file.isFile()) {
String name = file.getName();
JunkInfo info = null;
if (name.endsWith(".apk")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mApkInfo.mChildren.add(info);
mApkInfo.mSize += info.mSize;
} else if (name.endsWith(".log")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mLogInfo.mChildren.add(info);
mLogInfo.mSize += info.mSize;
} else if (name.endsWith(".tmp") || name.endsWith(".temp")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mTmpInfo.mChildren.add(info);
mTmpInfo.mSize += info.mSize;
}

if (info != null) {
mCallback.onProgress(info);
}
} else {
if (level < SCAN_LEVEL) {
travelPath(file, level + 1);
}
}
}
}


and LogCat error is :



 java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
at java.lang.Thread.run(Thread.java:764) 

Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)


getting error here for loop for a file scanner



Thank you in Advance for help i will appreciate your answer










share|improve this question
























  • I believe your root.listFiles() returned null, which means the root was not a directory.
    – Aaron
    Nov 22 at 9:31










  • yes you are right
    – Ram Mohan dubey
    Nov 22 at 9:33










  • in marshmallow it is working
    – Ram Mohan dubey
    Nov 22 at 9:36










  • Maybe it has a different directory structure.
    – Aaron
    Nov 22 at 9:37
















0














I am working on file scanner App and clear cache it is crash on activity . my application has clear cache like boost application, unstall apk from app Ram boost etc ........



 private void travelPath(File root, int level) {
if (root == null || !root.exists() || level > SCAN_LEVEL) {
return;
}

File lists = root.listFiles();
for (File file : lists) {
if (file.isFile()) {
String name = file.getName();
JunkInfo info = null;
if (name.endsWith(".apk")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mApkInfo.mChildren.add(info);
mApkInfo.mSize += info.mSize;
} else if (name.endsWith(".log")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mLogInfo.mChildren.add(info);
mLogInfo.mSize += info.mSize;
} else if (name.endsWith(".tmp") || name.endsWith(".temp")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mTmpInfo.mChildren.add(info);
mTmpInfo.mSize += info.mSize;
}

if (info != null) {
mCallback.onProgress(info);
}
} else {
if (level < SCAN_LEVEL) {
travelPath(file, level + 1);
}
}
}
}


and LogCat error is :



 java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
at java.lang.Thread.run(Thread.java:764) 

Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)


getting error here for loop for a file scanner



Thank you in Advance for help i will appreciate your answer










share|improve this question
























  • I believe your root.listFiles() returned null, which means the root was not a directory.
    – Aaron
    Nov 22 at 9:31










  • yes you are right
    – Ram Mohan dubey
    Nov 22 at 9:33










  • in marshmallow it is working
    – Ram Mohan dubey
    Nov 22 at 9:36










  • Maybe it has a different directory structure.
    – Aaron
    Nov 22 at 9:37














0












0








0







I am working on file scanner App and clear cache it is crash on activity . my application has clear cache like boost application, unstall apk from app Ram boost etc ........



 private void travelPath(File root, int level) {
if (root == null || !root.exists() || level > SCAN_LEVEL) {
return;
}

File lists = root.listFiles();
for (File file : lists) {
if (file.isFile()) {
String name = file.getName();
JunkInfo info = null;
if (name.endsWith(".apk")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mApkInfo.mChildren.add(info);
mApkInfo.mSize += info.mSize;
} else if (name.endsWith(".log")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mLogInfo.mChildren.add(info);
mLogInfo.mSize += info.mSize;
} else if (name.endsWith(".tmp") || name.endsWith(".temp")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mTmpInfo.mChildren.add(info);
mTmpInfo.mSize += info.mSize;
}

if (info != null) {
mCallback.onProgress(info);
}
} else {
if (level < SCAN_LEVEL) {
travelPath(file, level + 1);
}
}
}
}


and LogCat error is :



 java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
at java.lang.Thread.run(Thread.java:764) 

Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)


getting error here for loop for a file scanner



Thank you in Advance for help i will appreciate your answer










share|improve this question















I am working on file scanner App and clear cache it is crash on activity . my application has clear cache like boost application, unstall apk from app Ram boost etc ........



 private void travelPath(File root, int level) {
if (root == null || !root.exists() || level > SCAN_LEVEL) {
return;
}

File lists = root.listFiles();
for (File file : lists) {
if (file.isFile()) {
String name = file.getName();
JunkInfo info = null;
if (name.endsWith(".apk")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mApkInfo.mChildren.add(info);
mApkInfo.mSize += info.mSize;
} else if (name.endsWith(".log")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mLogInfo.mChildren.add(info);
mLogInfo.mSize += info.mSize;
} else if (name.endsWith(".tmp") || name.endsWith(".temp")) {
info = new JunkInfo();
info.mSize = file.length();
info.name = name;
info.mPath = file.getAbsolutePath();
info.mIsChild = false;
info.mIsVisible = true;
mTmpInfo.mChildren.add(info);
mTmpInfo.mSize += info.mSize;
}

if (info != null) {
mCallback.onProgress(info);
}
} else {
if (level < SCAN_LEVEL) {
travelPath(file, level + 1);
}
}
}
}


and LogCat error is :



 java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
at java.lang.Thread.run(Thread.java:764) 

Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.example.dell.testnew.custom.OverallScanTask.travelPath(OverallScanTask.java:35)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:85)
at com.example.dell.testnew.custom.OverallScanTask.doInBackground(OverallScanTask.java:15)
at android.os.AsyncTask$2.call(AsyncTask.java:333)


getting error here for loop for a file scanner



Thank you in Advance for help i will appreciate your answer







java android file android-asynctask






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:30

























asked Nov 22 at 9:24









Ram Mohan dubey

33528




33528












  • I believe your root.listFiles() returned null, which means the root was not a directory.
    – Aaron
    Nov 22 at 9:31










  • yes you are right
    – Ram Mohan dubey
    Nov 22 at 9:33










  • in marshmallow it is working
    – Ram Mohan dubey
    Nov 22 at 9:36










  • Maybe it has a different directory structure.
    – Aaron
    Nov 22 at 9:37


















  • I believe your root.listFiles() returned null, which means the root was not a directory.
    – Aaron
    Nov 22 at 9:31










  • yes you are right
    – Ram Mohan dubey
    Nov 22 at 9:33










  • in marshmallow it is working
    – Ram Mohan dubey
    Nov 22 at 9:36










  • Maybe it has a different directory structure.
    – Aaron
    Nov 22 at 9:37
















I believe your root.listFiles() returned null, which means the root was not a directory.
– Aaron
Nov 22 at 9:31




I believe your root.listFiles() returned null, which means the root was not a directory.
– Aaron
Nov 22 at 9:31












yes you are right
– Ram Mohan dubey
Nov 22 at 9:33




yes you are right
– Ram Mohan dubey
Nov 22 at 9:33












in marshmallow it is working
– Ram Mohan dubey
Nov 22 at 9:36




in marshmallow it is working
– Ram Mohan dubey
Nov 22 at 9:36












Maybe it has a different directory structure.
– Aaron
Nov 22 at 9:37




Maybe it has a different directory structure.
– Aaron
Nov 22 at 9:37












1 Answer
1






active

oldest

votes


















0














Files.listFiles can return null if it is not a directory:




Returns an array of abstract pathnames denoting the files and directories in
the directory denoted by this abstract pathname. The array will be
empty if the directory is empty. Returns null if this abstract
pathname does not denote a directory, or if an I/O error occurs.




If you do not expect it to be null, then you should probably revisit your code logic and find a workaround, otherwise you can easily handle it with a null check.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427585%2fworking-on-clear-cache-app-getting-error-in-an-asynctask-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Files.listFiles can return null if it is not a directory:




    Returns an array of abstract pathnames denoting the files and directories in
    the directory denoted by this abstract pathname. The array will be
    empty if the directory is empty. Returns null if this abstract
    pathname does not denote a directory, or if an I/O error occurs.




    If you do not expect it to be null, then you should probably revisit your code logic and find a workaround, otherwise you can easily handle it with a null check.






    share|improve this answer




























      0














      Files.listFiles can return null if it is not a directory:




      Returns an array of abstract pathnames denoting the files and directories in
      the directory denoted by this abstract pathname. The array will be
      empty if the directory is empty. Returns null if this abstract
      pathname does not denote a directory, or if an I/O error occurs.




      If you do not expect it to be null, then you should probably revisit your code logic and find a workaround, otherwise you can easily handle it with a null check.






      share|improve this answer


























        0












        0








        0






        Files.listFiles can return null if it is not a directory:




        Returns an array of abstract pathnames denoting the files and directories in
        the directory denoted by this abstract pathname. The array will be
        empty if the directory is empty. Returns null if this abstract
        pathname does not denote a directory, or if an I/O error occurs.




        If you do not expect it to be null, then you should probably revisit your code logic and find a workaround, otherwise you can easily handle it with a null check.






        share|improve this answer














        Files.listFiles can return null if it is not a directory:




        Returns an array of abstract pathnames denoting the files and directories in
        the directory denoted by this abstract pathname. The array will be
        empty if the directory is empty. Returns null if this abstract
        pathname does not denote a directory, or if an I/O error occurs.




        If you do not expect it to be null, then you should probably revisit your code logic and find a workaround, otherwise you can easily handle it with a null check.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 9:49

























        answered Nov 22 at 9:37









        Aaron

        1,7051212




        1,7051212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427585%2fworking-on-clear-cache-app-getting-error-in-an-asynctask-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Langue