循环测试结果为null,崩溃程序

时间:2014-11-26 18:14:29

标签: java arrays loops nullpointerexception

我一直在筛选我的代码来完成一个项目,并且不能按照它的设计来使用这种方法。应该发生的是系统搜索作为用户输入值的参数传递的数组。以下是编写的整个方法(我已经调试了一些随机文本):

    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++);

此问题现已解决。

1 个答案:

答案 0 :(得分:1)

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