首页 > 解决方案 > EmptyStackException in getting current path for file manager although initialize path

问题描述

I made a file manager app and when I try to run it I get EmptyStackException in my method getCurrentDirectory(). In another method I initilized stackPath and I'm sure it's not empty. Here's my code:

 public void initialisePathStackWithAbsolutePath(boolean choice, File file) {
    if (!choice)
        return;
    File dir = new File(String.valueOf(file));
    ArrayList<File> temp = new ArrayList<>();
    File root = new File("/");
    while (!dir.equals(root)) {
        dir.getParentFile();
        temp.add(dir);
    }
    Collections.reverse(temp);
    for (int i = 0; i < temp.size(); i++) {
        pathStack.push(temp.get(i));
    }
}

This is where I get error in logcat:

   public File getCurrentDirectory() {
    return pathStack.peek();
}

And this my variable:

 private Stack<File> pathStack;

标签: javaandroidpathstack

解决方案


推荐阅读