Home > Back-end >  How to refresh listview with files from subdirectory
How to refresh listview with files from subdirectory

Time:10-05

I want to create a file manager and I would like to get files from the subdirectory. When I click on listview item I want to show the files from subdirectory but I get NullPointerException.

Here is my code:

 ListView lv;
ArrayList<String> filenames=new ArrayList<String>();
ArrayAdapter<String> adapter;
String oldPathvalue=""
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
            PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE
        }, 17);

    } else {
        lv = findViewById(R.id.lvFiles);

        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,filenames);
        lv.setAdapter(adapter);
       getFiles(Environment.getExternalStorageDirectory() "/kepek/",null);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 //   String currentVal = (String) parent.getItemAtPosition(position);
adapter.clear();
adapter.notifyDataSetChanged();
 getFiles(Environment.getExternalStorageState()   "/kepek/Camera/","creditcardnumber");
                    adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,filenames);
                    lv.invalidateViews();
                    lv.setAdapter(adapter);


            }
        });
    }
}

Here is the getFiles method:

 public void getFiles(String pathname,String child){

    File root = new File(pathname);
      Toast.makeText(getApplicationContext(),pathname,Toast.LENGTH_LONG).show();
    String[] paths=root.list();

    if (paths != null) {
        for (int i = 0; i < paths.length; i  ) {
            //if(!paths[i].isDirectory()) {
                filenames.add(paths[i]);
            //}
            //else{

            //}

        }
    }
    else{
       pathname=pathname child;
       File root1=new File(pathname);
       String[] paths1=root1.list();
       for(int i=0;i<paths1.length;i  ){
           filenames.clear();
           filenames.add(paths[i]);
       }
    }

}

Here is the stacktrace

     Process: com.example.myapplication, PID: 32379
java.lang.NullPointerException: Attempt to get length of null array
    at com.example.myapplication.MainActivity.getFiles(MainActivity.java:94)
    at com.example.myapplication.MainActivity$1.onItemClick(MainActivity.java:50)
    at android.widget.AdapterView.performItemClick(AdapterView.java:318)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
    at android.widget.AbsListView$3.run(AbsListView.java:4052)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6702)
    at java.lang.reflect.Method.invoke(Native Method)
    at 

com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

CodePudding user response:

if (paths != null)

Ok. But you also need:

if (paths1 != null)

As now you use paths1.length when paths1==null.

CodePudding user response:

IngetFiles(Environment.getExternalStorageDirectory() "/kepek/",null);
you're passing null as child parameter to method getFiles() but then inside the method doing pathname=pathname child; where child is Null and shouldn't be used in anything.

Accessing or attempting to invoke anything on Null generates a NullPointerException.

If you want your child to be an empty string pass "" instead.

getFiles(Environment.getExternalStorageDirectory() "/kepek/","");
  • Related