gpt4 book ai didi

java - 循环测试结果为空,程序崩溃

转载 作者:行者123 更新时间:2023-12-01 12:13:19 24 4
gpt4 key购买 nike

我一直在筛选项目的代码,但无法让此方法按设计的方式工作。应该发生的是系统搜索作为参数传递的数组以查找用户输入的值。以下是编写的整个方法(我添加了一些随机文本用于调试):

    public static void updatePC(int ARRAY_SIZE, int count, String[] customerName, String[] customerID, String[] os, String[] typeOfProblem, int[] turnAroundTime) {
Scanner keyboard = new Scanner(System.in);
String toUpdate;
String[] customerIDClone = new String[count];
int intToUpdate = -1, loop = count, n;
char correct = 'n';

customerIDClone = Arrays.copyOf(customerID, count);
while (correct == 'n') {
System.out.println("Please enter the customer ID of the work order you wish to modify");
toUpdate = keyboard.nextLine();
if (Arrays.asList(customerIDClone).contains(toUpdate) == true) {
intToUpdate = Integer.parseInt(toUpdate);
}
while (Arrays.asList(customerIDClone).contains(toUpdate) == false) {
System.out.println("The customer ID you entered was not found. Please try again.");
toUpdate = keyboard.nextLine();
intToUpdate = Integer.parseInt(toUpdate);
}
System.out.println("Beginning search loop. The value to find is " + toUpdate);
for (n = 0; n < loop; n++);
{
System.out.println("Loop loop looooooooop");
System.out.println(customerID[n]);
System.out.println(customerID[0]);
if (customerID[count].equals(toUpdate)) {
System.out.println("Found it!");
count = n;
}
}

System.out.println("Testing run 1.");
}
System.out.println("");
System.out.println("--");
System.out.println(" Name: " + customerName[count]);
System.out.println(" Customer ID: " + customerID[count]);
System.out.println(" OS: " + os[count]);
System.out.println(" Type of Problem: " + typeOfProblem[count]);
System.out.println("Expected Turnaround Time: " + turnAroundTime[count]);
System.out.println("--");
System.out.println("Is this new record correct? Please enter y or n. ");
correct = keyboard.next().charAt(0);
keyboard.nextLine();
}

我一直在网站和其他地方整理其他问题,但我无法找出问题所在。该方法的输出为

Please enter the customer ID of the work order you wish to modify
3
Beginning search loop. The value to find is 3
Loop loop looooooooop
null
1
Exception in thread "main" java.lang.NullPointerException
at mckelvey_project3.McKelvey_Project3.updatePC(McKelvey_Project3.java:183)
at mckelvey_project3.McKelvey_Project3.main(McKelvey_Project3.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)

我写了这行

System.out.println(customerID[0]);

进入我的代码只是为了演示并确保数组已定义并初始化。另外,

System.out.println(customerID[1]);

也已定义。看来问题存在于我的循环测试中;正在运行

System.out.println(loop + count + n)

表明它们都等于 3。n 在哪里设置为 3?

编辑添加了 @hfontanez 建议的以下代码

        customerIDClone = Arrays.copyOf(customerID, count);
for (int i = 0; i < count; i++) {
if (customerIDClone[i] == null) {
System.out.println("Element " + i + " is null. Creating new String.");
customerIDClone[i] = new String();
}

输出如下:

Please enter the customer ID of the work order you wish to modify
3
Beginning search loop. The value to find is 3
Loop loop looooooooop
null
2
Exception in thread "main" java.lang.NullPointerException
at mckelvey_project3.McKelvey_Project3.updatePC(McKelvey_Project3.java:190)
at mckelvey_project3.McKelvey_Project3.main(McKelvey_Project3.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)

编辑2我发现问题了……我有一只流浪的;这里:

for (n = 0; n < count; n++);

此问题现已解决。

最佳答案

Java 中的数组是一个对象。创建对象数组(例如字符串数组)时,必须初始化数组和每个元素。该行:

String[] customerIDClone = new String[count];

初始化一个由“COUNT”个 String 对象组成的数组,但每个元素仍然为 null。这就是为什么您会收到空指针异常。

在调用 customerID[count].equals(toUpdate) 之前,必须正确实例化 customerID[count] 处的 (String) 元素。

这种情况下的问题是新数组不是原始数组的精确副本。这一行:

customerIDClone = Arrays.copyOf(customerID, count);

创建一个长度为“COUNT”的新数组。如果“COUNT”大于原始长度,则所有新创建的元素都将用 NULL 填充。这解释了为什么 customerID[0] 有效,但 customerID[n] 无效。要解决此问题,您可以迭代新数组并向每个 NULL 元素添加一个 new String()

更新:

试试这个:

customerIDClone = Arrays.copyOf(customerID, count);
for (int i = 0; i < count; i++)
{
if(customerIDClone[i] == null)
{
System.out.println("Element " + i + " is null. Creating new String.");
customerIDClone[i] = new String();
}
}

关于java - 循环测试结果为空,程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27156180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com