NullPointerException - Attempt to get length of null array (readDirectory())

前端 未结 2 1574
甜味超标
甜味超标 2021-01-01 22:05

I have the code as shown here. My Problem is a NullPointerException at files.length

for(int i=0; i < files.length; i++){

It is caused b

相关标签:
2条回答
  • 2021-01-01 22:34
    f.listFiles()
    

    will return null if the path does not exist.

    Please check your path.

    Update

    Android will also require a permission to read certain files. Maybe you need to add this to your manifest:

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    More information:

    http://developer.android.com/reference/android/Manifest.permission.html

    0 讨论(0)
  • 2021-01-01 22:34

    i have check the condition

    File folderPath = new File(getFilesDir() + "/" + getResources().getString(R.string.app_name));

        if (folderPath.exists()) {}
    

    for my code and it works for me becuase when this file attemp to read data there were no directory exist on the storage so that it was giving error.

    now works fine

    0 讨论(0)
提交回复
热议问题