I've been sifting through my code for a project and can't get this method to work as it's designed. What should be happening is that the system searches an array passed as a parameter for a user-inputted value. The following is the entire method as is written (there is some random text I've thrown in for debugging):
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();
}
I've been sorting through other questions here on the site and elsewhere, but I can't figure out the issue. The output of the method is
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)
I wrote the line
System.out.println(customerID[0]);
into my code just to demonstrate and make sure that the array is defined and initialized. Additionally,
System.out.println(customerID[1]);
is also defined. It seems the problem exists within my loop test; running
System.out.println(loop + count + n)
reveals that they are all equal to 3. Where is n getting set to 3 at?
EDIT Added the following code suggested by @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();
}
and the output is as follows:
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)
EDIT 2 I found the problem... I had a stray ; here:
for (n = 0; n < count; n++);
This issue is now solved.