-2

I am trying to show cache size files in MB, used by my App. but my app crashing. Error : Caused by: java.lang.NullPointerException

private void initializeCache() {
    long size = 0;
    size += getDirSize(this.getCacheDir());
    size += getDirSize(this.getExternalCacheDir());
    this.text_view_cache_value.setText(getResources().getString(R.string.label_cache)  + readableFileSize(size));

}

public long getDirSize(File dir){
    long size = 0;
    for (File file : dir.listFiles()) {
        if (file != null && file.isDirectory()) {
            size += getDirSize(file);
        } else if (file != null && file.isFile()) {
            size += file.length();
        }
    }
    return size;
}
Sambit Mallick
  • 155
  • 4
  • 14
SK3
  • 37
  • 4

1 Answers1

1

Set a debug point or do a System.out.println(..) to check the content of :

this.getCacheDir()
his.getExternalCacheDir()
this.text_view_cache_value
R.string.label_cache

one of them has a null value.

Marc Le Bihan
  • 2,308
  • 2
  • 23
  • 41
  • That is really more of a comment. Surprising, that somebody upvoted it. – GhostCat Sep 09 '18 at 15:02
  • Sorry, at the time I wrote it, I did not had enough reputation points to write a comment but only a response. I think that one of those variables is null and that my response is accurate provided the information the opener of the post, a beginner, gave. What the trouble about helping him ? – Marc Le Bihan Sep 09 '18 at 15:11