【发布时间】:2014-11-26 18:14:29
【问题描述】:
我一直在为一个项目筛选我的代码,但无法让这种方法按照设计的方式工作。应该发生的是系统搜索作为参数传递的数组以查找用户输入的值。以下是所写的整个方法(有一些随机文本我已经抛出用于调试):
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++);
这个问题现在已经解决了。
【问题讨论】:
-
顺便说一句,
if(Arrays.asList(customerIDClone).contains(toUpdate) == true)与if(Arrays.asList(customerIDClone).contains(toUpdate))相同 -
啊,谢谢你的提示。现在更改我的代码。这也适用于 while (Arrays.asList(customerIDClone).contains(toUpdate) == false) 能够写成 while (!Arrays.asList(customerIDClone).!contains(toUpdate)) 吗?
-
也改变这个:
while (Arrays.asList(customerIDClone).contains(toUpdate) == false)到while (!Arrays.asList(customerIDClone).contains(toUpdate)) -
好的,很酷。愚蠢的问题,但我似乎无法让 cmets 中的代码工作的四个空格格式..我错过了什么?
-
将代码用重音符号(波浪号)括在 #1 键的左侧
标签: java arrays loops nullpointerexception