错误消息:文件 "countries.geo.json" 丢失或无法访问

Error message: The file "countries.geo.json" is missing or inaccessible

我正在 Java class 学习 Coursera OOP。在模块4作业中,我运行课程提供的代码在EarthquakeCityMap.java, 我得到一个错误,因为 "The file "countries.geo.json" 丢失或无法访问,请确保 URL 有效或文件已添加到您的草图中并且可读。 线程异常 "Animation Thread" java.lang.NullPointerException"

我尝试将 countryFile 设置为 "../data/countries.geo.json", "data/countries.geo.json", 以及国家文件的完整路径, 还是没有解决问题

//this error points to the code
private String countryFile = "countries.geo.json";
List<Feature> countries = GeoJSONReader.loadData(this, countryFile);"

//the countries file is saved in data folder.

项目文件夹列表

如果您指定了完整路径但它不起作用,您可能忘记了用另一个反斜杠转义 \ 字符。反斜杠字符很特殊,需要加倍才能正确解释 windows 路径。例如“c:\\users\\...”。您也可以指定 / 而不是 \ 并且它会起作用:“c:/users/...”

也就是说,相对于(IE 不是文件系统根目录的绝对路径)文件的路径解析是相对于执行的应用程序的工作目录。通常,在没有任何特殊配置的 IDE 中,工作目录将是项目的根路径。因此,为了正确解析相对文件路径,您必须将路径指定为“data/countries.geo.json”.

您还可以通过执行 System.out.println(new java.io.File(“.”).getAbsolutePath()) 和根据这个文件夹制作相对路径。

"countries.geo.json"(除非在 GeoJSONReader 中更改操作此路径)将相对于 IntelliJ 项目 out 中已编译的 java .class 文件文件夹。

如果 GeoJSONReader.loadData(this, countryFile); 中的 this 是一个 PApplet 实例,您可以使用 sketchPath() 使该路径相对于运行草图的文件夹:

List<Feature> countries = GeoJSONReader.loadData(this, this.sketchPath("data"+File.separator+countryFile));

以上代码段基于一个假设,因此您的代码中的语法可能略有不同,但希望这能说明您将如何使用 sketchPath()

此外还有一个 dataPath(),您可以从 setup() 中的主 PApplet 进行测试:

String fullJSONPath = dataPath("countries.geo.json");
println("fullJSONPath: " + fullJSONPath);//hopefully this prints the full path to the json file on your machine
println(new File(fullJSONPath).exists());//hopefully this prints true