-4

I am building a simple file explorer and showing files on ListView using simple_list_item_1 and onItemClick(AdapterView parent, View view, int position, long id) this oncItemClick works fine for all all other folder clicks but on clicking last two items my application crashing and getting the following error
java.lang.ArrayIndexOutOfBoundsException: length=2; index=9 please take a look at code whats wrong

public class UploadActivity extends AppCompatActivity {
private ListView listView;
protected File[] files;
protected LinkedList<String> pathList;
protected String path="/sdcard";
protected final String generalPath="/sdcard";
protected Bundle bundle;
private View emtyFolder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload);
    pathList=new LinkedList<>();
    bundle=getIntent().getExtras();
    listView=(ListView)findViewById(R.id.local_files_listView);
    emtyFolder=getLayoutInflater().inflate(R.layout.emty_folder,null);



    openPath(path);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (files[position].isDirectory())
            {
                Log.e("Folder Name",files[position].getName());
                if(files[position].listFiles().length>0) {
                    for (int i=0; i<files[position].listFiles().length;i++){
                        Log.e("File ",files[i].getName());
                    }
                    String newpath = files[position].getName();
                    forwardPath(newpath);
                    openPath(path);
                }
                else {
                    emtyFolder();
                    String newpath = files[position].getName();
                    forwardPath(newpath);
                    listView.setEmptyView(emtyFolder);
                }

            }
            if (files[position].isFile()) {
                Log.e("Is a file","True");
                String host=bundle.get("host").toString();
                String user=bundle.get("user").toString();
                String password=bundle.get("password").toString();
                int port= Integer.parseInt(bundle.get("port").toString());
                String remote=bundle.get("remote").toString();
                Transaction transaction=new 
                Transaction(host,user,password,port,getApplicationContext());
                try {
                    Log.e("Upload","Starting");
                    boolean status=transaction.Upload(remote, new File(path + 
              "/" + files[position].getName()));
                    Log.e("Upload", String.valueOf(status));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

public void backWardPath() {
    if (!pathList.isEmpty()) {
        this.path = generalPath;
        pathList.removeLast();
        for (int i = 0; i < pathList.size(); i++) {
            this.path = this.path + pathList.get(i);
        }
    }

    openPath(path);
}

public void forwardPath(String newPath) {
        this.path = generalPath;

    pathList.add("/" + newPath);
    for (int i = 0; i < pathList.size(); i++) {
        this.path = this.path + pathList.get(i);
    }
}

@Override
public void onBackPressed() {
    setContentView(R.layout.activity_upload);
    Log.e("Path",path);
    backWardPath();

}


public void openPath(String path) {
    File file = new File(path);
    files = file.listFiles();
    ArrayAdapter<File> arrayAdapter=new ArrayAdapter<File>(this,android.R.layout.simple_list_item_1,files);
    listView.setAdapter(arrayAdapter);
}


private void emtyFolder(){
    Toast.makeText(this,"Empty Folder",Toast.LENGTH_LONG).show();
   }

}

enter image description here

enter image description here

Imran Shad
  • 58
  • 13

2 Answers2

0

The compiler tells you that you try to access to an index out of range of your array.

java.lang.ArrayIndexOutOfBoundsException: length=2; index=9

The length of your array is 2 and you try to access with a index of 9. Check the line that compiler tells you where the problem occur.

Itoun
  • 3,713
  • 5
  • 27
  • 47
0

This part:

for (int i=0; i<files[position].listFiles().length;i++){
    Log.e("File ",files[i].getName());
}

looks very suspicious. There's absolutely no relation between the number of files in the directory indicated by files[position] and the number of files in files. Probably you need another array of File objects.

SeverityOne
  • 2,476
  • 12
  • 25