所以我试图用BufferedReader编写一个循环来读取下一行,将该行转换为double,然后将其添加到总和中。这是继续到文件的末尾。
出于某种原因,我不断收到此错误,
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1017)
at java.lang.Double.parseDouble(Double.java:540)
以下是我的代码:
while(bReader.readLine() != null)
{
line = bReader.readLine();
double num = Double.parseDouble(line);
sum = sum + num;
count++;
}
答案 0 :(得分:3)
您在while循环中读取了一个新行,返回null:
while(bReader.readLine() != null)
{
line = bReader.readLine(); // read a second line
double num = Double.parseDouble(line);
sum = sum + num;
count++;
}
更改为:
while((line = bReader.readLine()) != null)
{
double num = Double.parseDouble(line);
sum = sum + num;
count++;
}