Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.nullpointer异常? 导入java.io.File; 导入java.io.IOException; 导入java.util.Scanner; 公营手机{ 公共静态void main(字符串Args[])引发IOException{ 扫描仪s=新的扫描仪(System.in); 文件输入=新文件(“mobile.txt”); 扫描仪f=新扫描仪(输入); 字符串[]单元格=新字符串[20]; Double[]金额=新的Double[20]; 双阈值=打印菜单(); 整数=0; while(f.hasNext()){ 单元格[数目]=f.next(); 金额[数量]=f.nextDouble(); 数字++; } System.out.println(“编号\tArmount”); for(int i=0;ithreshold)//这是空指针的位置 //发生异常 { 系统输出打印(单元格[i]+“\t”+金额[i]); } } } 静态双打印菜单(){ 扫描仪s=新的扫描仪(System.in); System.out.print(“输入文件名:”); 字符串文件名=s.nextLine(); 系统输出打印(“单元格账单阈值:”); 双阈值=s.nextDouble(); 返回阈值; } }_Java_Arrays - Fatal编程技术网

java.lang.nullpointer异常? 导入java.io.File; 导入java.io.IOException; 导入java.util.Scanner; 公营手机{ 公共静态void main(字符串Args[])引发IOException{ 扫描仪s=新的扫描仪(System.in); 文件输入=新文件(“mobile.txt”); 扫描仪f=新扫描仪(输入); 字符串[]单元格=新字符串[20]; Double[]金额=新的Double[20]; 双阈值=打印菜单(); 整数=0; while(f.hasNext()){ 单元格[数目]=f.next(); 金额[数量]=f.nextDouble(); 数字++; } System.out.println(“编号\tArmount”); for(int i=0;ithreshold)//这是空指针的位置 //发生异常 { 系统输出打印(单元格[i]+“\t”+金额[i]); } } } 静态双打印菜单(){ 扫描仪s=新的扫描仪(System.in); System.out.print(“输入文件名:”); 字符串文件名=s.nextLine(); 系统输出打印(“单元格账单阈值:”); 双阈值=s.nextDouble(); 返回阈值; } }

java.lang.nullpointer异常? 导入java.io.File; 导入java.io.IOException; 导入java.util.Scanner; 公营手机{ 公共静态void main(字符串Args[])引发IOException{ 扫描仪s=新的扫描仪(System.in); 文件输入=新文件(“mobile.txt”); 扫描仪f=新扫描仪(输入); 字符串[]单元格=新字符串[20]; Double[]金额=新的Double[20]; 双阈值=打印菜单(); 整数=0; while(f.hasNext()){ 单元格[数目]=f.next(); 金额[数量]=f.nextDouble(); 数字++; } System.out.println(“编号\tArmount”); for(int i=0;ithreshold)//这是空指针的位置 //发生异常 { 系统输出打印(单元格[i]+“\t”+金额[i]); } } } 静态双打印菜单(){ 扫描仪s=新的扫描仪(System.in); System.out.print(“输入文件名:”); 字符串文件名=s.nextLine(); 系统输出打印(“单元格账单阈值:”); 双阈值=s.nextDouble(); 返回阈值; } },java,arrays,Java,Arrays,所以我要做的是从一个文件中读入数据,将数据存储在两个数组中,如果数组值大于为阈值变量输入的值,则打印出数组。但是当我尝试运行程序时,会弹出空指针错误,知道为什么吗?如果阈值小于20,for循环将继续到Amounts数组的末尾,其中将包括未初始化的double。根据您需要的功能,我建议循环,直到I问题是您读取的记录少于20条 数组中的每个Double都默认为null值。当java为了比较Amounts[i]和threshold而取消装箱时,它会尝试取消引用该空值,从而创建异常 解决方案是标记成功读

所以我要做的是从一个文件中读入数据,将数据存储在两个数组中,如果数组值大于为阈值变量输入的值,则打印出数组。但是当我尝试运行程序时,会弹出空指针错误,知道为什么吗?

如果阈值小于20,for循环将继续到Amounts数组的末尾,其中将包括未初始化的double。根据您需要的功能,我建议循环,直到I问题是您读取的记录少于20条

数组中的每个Double都默认为null值。当java为了比较
Amounts[i]
threshold
而取消装箱时,它会尝试取消引用该空值,从而创建异常


解决方案是标记成功读入的值的数量,并仅将该数量的值与阈值进行比较。

无法保证文件数据的计数为20

所以改变你的循环限制计数器

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;
    }
}

//for(int i=0;i请分享你的堆栈跟踪我该怎么做?。很抱歉,我刚刚开始编程,对编程了解不多yet@user1840435在比较之前,我首先在for循环中打印出
Amounts[I]
I
,以验证空
Amounts[I]
导致空指针异常(我可能总是错的)。似乎您已经有了一个变量来标记成功读取了多少值
number
,只需将
更改为(int I=0;I
//for(int i=0;i<Amounts.length;i++)
for(int i=0;i<number;i++)  //variable number has a count of file's data