【问题标题】:Loop test results in null, crashes program [duplicate]循环测试结果为空,程序崩溃[重复]
【发布时间】: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


【解决方案1】:

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

String[] customerIDClone = new String[count];

初始化一个包含 'COUNT' 个 String 对象的数组,但每个元素仍然为空。这就是你得到空指针异常的原因。

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

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

customerIDClone = Arrays.copyOf(customerID, count);

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

更新

试试这个:

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

【讨论】:

  • 但是不是customerIDClone = Arrays.copyOf(customerID, count);初始化的
  • 另外,我的搜索循环使用 customerID 而不是 customerIDClone
  • 否... 如果新数组大于原始数组,则用 NULL 填充新元素。请参阅API Arrays.copyOf()。在您的情况下,原始数组似乎只有一个元素;这就是customerID[0] 起作用的原因。
  • 刚刚测试了这个理论,但是customerID[1] & customerID[2] 都被正确定义了。此外,即使第一次循环测试通过,该值仍然列为'null'
  • 这不是理论。这是 API 规定的事实。转到我上面提供的几个 cmets 的链接。
猜你喜欢
  • 2011-09-12
  • 1970-01-01
  • 2013-11-26
  • 2011-01-09
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多