hedzr / android-file-chooser

a lightweight file/folder chooser or picker
Apache License 2.0
284 stars 62 forks source link

Crash on Fragment #17

Closed sajid-verveba closed 6 years ago

sajid-verveba commented 6 years ago

Crash Error : -

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File[] java.io.File.listFiles(java.io.FileFilter)' on a null object reference
                                                                        at com.obsez.android.lib.filechooser.ChooserDialog.listDirs(ChooserDialog.java:288)
                                                                        at com.obsez.android.lib.filechooser.ChooserDialog.refreshDirs(ChooserDialog.java:371)
                                                                        at com.obsez.android.lib.filechooser.ChooserDialog.build(ChooserDialog.java:204)
                                                                        at com.verveba.pimapp.view.frgmnts.LocTestingWorstPortFragment.chooseDTPFile(LocTestingWorstPortFragment.java:304)

Following code used in fragment : -

new ChooserDialog().with(getActivity())
                .withFilter(false, false, "jpg", "jpeg", "png")
                .withResources(R.string.dtp_file, R.string.title_choose, R.string.dialog_cancel)
                .withChosenListener(new ChooserDialog.Result() {
                    @Override
                    public void onChoosePath(String path, File pathFile) {

                        }
                    }
                })
                .build()
                .show();
hedzr commented 6 years ago

add .withStartFile(startingDir)

angujo commented 6 years ago

Experiencing the same here. Log is;

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File[] java.io.File.listFiles(java.io.FileFilter)' on a null object reference at com.obsez.android.lib.filechooser.ChooserDialog.listDirs(ChooserDialog.java:288) at com.obsez.android.lib.filechooser.ChooserDialog.refreshDirs(ChooserDialog.java:371) at com.obsez.android.lib.filechooser.ChooserDialog.build(ChooserDialog.java:204)

My code is:

 btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ChooserDialog().with(getActivity())
                        .withStartFile("")
                       // .withStartFile(Environment.getExternalStorageState()+"/")
                        .withFilterRegex(false, false, ".*\\.(jpe?g|png)")
                        .withChosenListener(new ChooserDialog.Result() {
                            @Override
                            public void onChoosePath(String path, File pathFile) {
                                Toast.makeText(getContext(), "FILE: " + path, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .build()
                        .show();
.............................

Kindly assist. EDIT: It failed even with .withStartFile(Environment.getExternalStorageState()+"/")

hedzr commented 6 years ago

it seems that caused by _currentDir is null, in ChooserDialog.java#L312: File[] files = _currentDir.listFiles(_fileFilter);, with an earlier version used. so:

hedzr commented 6 years ago

just checked: withStartFile("") cause _currentDir == null. as a workaround, replace it with withStartFile(null) or .withStartFile(Environment.getExternalStorageDirectory().getAbsolutePath()).