我正在研究加州大学圣地亚哥分校的OOP课程。我必须获取一个CVS文件,然后处理它并将日期解析为他们给出的第二个赋值的哈希映射。但是当我尝试加载CSV文件时,似乎Java无法访问它。
package app;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.providers.Google.GoogleMapProvider;
import de.fhpotsdam.unfolding.utils.MapUtils;
import processing.core.PApplet;
public class LifeExpectancy extends PApplet {
//properties
UnfoldingMap map;
GoogleMapProvider provider;
File csvFile;
//setup method
//it only runs once, so write your code accordingly
public void setup() {
csvFile = new File("/data/LifeExpectancyWorldBank.csv");
try {
loadLifeExpectancyfromCSV(csvFile.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
size(800, 600, OPENGL);
provider = new GoogleMapProvider();
map = new UnfoldingMap(this, 50, 50, 700, 500, provider);
MapUtils.createDefaultEventDispatcher(this, map);
}
//draw method
//it is dynamic, that means it runs through the program's runtime
//so write your code accordingly
public void draw() {
map.draw();
}
//methods
/**
* Loads a CSV file, parses it and returns the parsed data accordingly
* @param fileName name of the file that will be loaded
* @return parsed data that comes from the file
*/
private Map<String, Float> loadLifeExpectancyfromCSV(String fileName) {
Map<String, Float> lifeExpMap = new HashMap<String, Float>();
String[] rows = loadStrings(fileName);
for (String row : rows) {
String[] columns = row.split(",");
float value = Float.parseFloat(columns[5]);
lifeExpMap.put(columns[4], value);
}
return lifeExpMap;
}
}
当我运行此代码时,会发生这种情况:
The file "/data/LifeExpectancyWorldBank.csv" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
Exception in thread "Animation Thread" java.lang.NullPointerException
at app.LifeExpectancy.loadLifeExpectancyfromCSV(LifeExpectancy.java:59)
at app.LifeExpectancy.setup(LifeExpectancy.java:27)
at processing.core.PApplet.handleDraw(PApplet.java:2361)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256)
at java.lang.Thread.run(Thread.java:745
我错过了什么?