import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class ReadCellPhones {
public static void main(String Args[]) throws IOException {
Scanner s = new Scanner(System.in);
File Input = new File("Cellphone.txt");
Scanner f = new Scanner(Input);
String[] Cells = new String[20];
Double[] Amounts = new Double[20];
Double threshold = printmenu();
int number = 0;
while (f.hasNext()) {
Cells[number] = f.next();
Amounts[number] = f.nextDouble();
number++;
}
System.out.println("NUMBER\tArmount");
for (int i = 0; i < Amounts.length; i++) {
if (Amounts[i] > threshold)// THIS IS WHERE THE NULLPOINTER
// EXCEPTION OCCURS
{
System.out.print(Cells[i] + "\t" + Amounts[i]);
}
}
}
static Double printmenu() {
Scanner s = new Scanner(System.in);
System.out.print("Enter the filename: ");
String Filename = s.nextLine();
System.out.print("Cell Bill Threshold: ");
Double threshold = s.nextDouble();
return threshold;
}
}
所以我要做的是从文件中读取数据,将数据存储在 2 个数组中,如果 Amounts 数组值大于为阈值变量输入的值,则打印出数组。但是当我尝试运行程序时,会弹出空指针错误,知道为什么吗?