我当前的程序从文件读取将该数据添加到客户对象,然后将该数据添加到列表中。我想读取第二个文件,它还会将数据添加到我的客户对象中。客户评级文件,但并非所有客户都有评级。这是我到目前为止所做的:
public void readFromFile()
{
try
{
BufferedReader inF = new BufferedReader(new FileReader("Customers.txt"));
BufferedReader inFR = new BufferedReader(new FileReader("Ratings.txt"));
String s = inF.readLine();
String s2 = inFR.readLine();
while(s != null)
{
String z[] = s.split(",");
String y[] = s2.split(" ");
Customer c = new Customer(z[0], z[1], z[2], Integer.parseInt(z[3]),z[4]);
if(c.getCustomerNr().equals(y[0]))
{
c.setRating(Integer.parseInt(y[1]));
}
else
{
c.setRating(0);
}
myList.add(c);
s = inF.readLine();
s2 = inFR.readLine();
}
}
catch (FileNotFoundException ex)
{
System.out.println("File does not exsit.");
System.exit(0);
}
catch (IOException ex)
{
System.out.println("Could not find line");
System.exit(0);
}
}
然而,我一直收到错误"空指针异常"在我的分裂方法s2。我不确定我做错了什么。如果有人可以给我一个关于我需要去哪里的提示,那也是很棒的,因为我想尽我所能去尝试自己解决问题。但是,如果你提供答案,我也会很高兴。
由于
答案 0 :(得分:1)
您的代码假定文件中的行数相同,而显然"Ratings.txt"
的行数较少。
我的建议是在单独的循环中处理文件。在阅读评级文件时,使用正确的客户编号在客户列表中搜索客户。