以下代码打开一个文本文件,并使用逗号作为分隔符将其内容分解为标记。我使用了与此类似的代码,它运行良好,但由于某种原因,这会在“myItems[counter].name=myTokens[counter];”上抛出 NullPointerException
public static serial[] getSerials(String fName) throws Exception {
String total = null;
BufferedReader lineReader = null;
lineReader = new BufferedReader(new FileReader(fName));
while ((lineReader.readLine()) != null) {
total = total + lineReader.readLine() + ",";
} //end while
String[] myTokens = total.split(","); /*break the string csv tokens*/
/*Put together the serial objects*/
serial mySerials[] = new serial[myTokens.length];
int counter = 0;
while (counter < myTokens.length) {
mySerials[counter].name = myTokens[counter];
counter++;
mySerials[counter - 1].onlineISSN = myTokens[counter];
counter++;
}
return mySerials;
} //end getSerials()