【发布时间】:2023-03-12 05:34:01
【问题描述】:
这部分代码有问题。从文件夹中,我正在加载所有图像,然后将其作为对象的一部分保存到这些对象的列表中。
当我在 Intellij 中运行它时,一切正常。我打印 isDirectory() 只是因为这个问题。在 intellij 中它返回 true。
但是当我尝试通过 jar 在 cmd 中运行时,就会出现问题。方法 isDirectory() 返回 false 并且 listFiles() 抛出 NullPointerException。
我也尝试手动将整个路径保存在字符串目录中,但没有任何改变。在 intellij 中,它运行完美,并且在 CMD 中带有 jar 的问题。问题确实在这。在我开始使用这些功能之前,Jar 工作得很好。感谢您的任何想法。
private void loadAllExercises(){
public String directory = System.getProperty("user.dir") + "\\pictures\\";
File directory = new File(directory);
System.out.println(directory.isDirectory());
for (File file : directory.listFiles()) {
loadExercise(file);
}
}
private void loadExercise(File file){
if(file.getName().toLowerCase().endsWith(".jpg")) {
Exercise exercise = new Exercise(file.getName());
this.allExercises.add(exercise);
}
}
【问题讨论】: