java - 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;
解决方案
推荐阅读
- c - 逐个字符读取 - 总是到达一个无效字符
- android - 在后台执行无法返回正确的活动页面
- c# - 视图中不直接使用的类仍然是模型(MVVM)吗?
- macos - 在 MacOS Sierra 10.12 上使用 brew 安装 php7.4 时出错
- amazon-web-services - AWS Elasticsearch 索引内存使用问题
- javascript - 如何检测从 Websocket 服务器发送的消息
- r - 滚动计算 data.table 中的后续或先前值
- scala - 如何在 Scala 中使用过滤器搜索地图并更新值
- c# - 用于创建 Word 文档的线程同步
- c - 使用 C 中的合并算法对字符串矩阵进行排序(按字母顺序)