I am trying to make an app and for that I need to list files. I have found some useful code and tailored it a bit for my needs. This is my (simple) code:
// List all files from specific directory
public void listFiles(){
// Path to directory to scane
String path = Environment.getExternalStorageDirectory().toString()+"/Download/";
// New file-instance
File f = new File(path);
// Get the textview to display message
TextView textview = (TextView) findViewById(R.id.textView);
textview.setText(f.toString());
// List the files
File file[] = f.listFiles();
for(int i = 0; i < file.length; i++){
Log.d("Files", "Filename: " + file[i].getName());
}
}
I know that this is caused by file.length in my for loop, but I don't understand why I get this error since it grabs the files in the right directory and I have files within that directory...
E
dit: This is my stack trace
java.lang.NullPointerException: Attempt to get length of null array
at com.example.filemaker.MainActivity.listFiles(MainActivity.java:79)
at com.example.filemaker.MainActivity$1.onClick(MainActivity.java:31)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
pathvariable isn't set up correctly?READ_EXTERNAL_STORAGEpermission?