【发布时间】:2014-03-07 17:56:06
【问题描述】:
对于学校,我必须编写一个程序,该程序将从文件中获取值,然后将它们打印出来。我使用的是HashMap,因为它最适合存储我的数据。我遇到的问题是,当我尝试添加它时,我得到一个空指针。也许这是因为整数没有被实例化,但我该怎么做呢?
这是我的代码:
public class Driver {
static ArrayList<Event> myEvents = new ArrayList<Event>();
static String[][][] counts = new String[50][1000][2];
//static ArrayList<int>[] myCounts = new ArrayList[];
//static ArrayList<String>[] myCountProfile = new ArrayList[];
@SuppressWarnings("unchecked")
static HashMap<String, Integer>[] count = new HashMap[50];
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
File input = new File("./src/Problem1/stormdata_2013.csv");
Scanner fileRead = new Scanner(input);
while (fileRead.hasNextLine()) {
String currentLine = fileRead.nextLine();
Scanner lineScan = new Scanner(currentLine).useDelimiter(",");
// Dealing with unnecessary information
for (int i = 0; i < 8; i++) {
lineScan.next();
}
String state = lineScan.next().trim();
int stateNum = Integer.parseInt(lineScan.next());
lineScan.next();
String month = lineScan.next().trim();
String type = lineScan.next().trim();
myEvents.add(new Event(type, month, state, stateNum));
count[stateNum].put(type,count[stateNum].get(type)+1);
}
}
}
【问题讨论】:
-
天哪。我不知道从哪里开始。我建议您使用调试器并将您的问题缩小到更具体的问题,然后编辑您的问题
-
您在问题中键入的程序甚至无法编译。
-
在询问错误时,请务必发布整个错误消息,包括任何异常的堆栈跟踪,并指出它正在谈论哪一行。
-
为避免输入错误,请在将输入字符串解析为 int 的行周围使用 try/catch。
-
怎么可能有人将其关闭为“太宽泛”?