I am getting a null pointer exception when i run this bubble sort and i'm not sure why, can someone please explain the problem?
for(int i = 1; i < clipArray.length; i++) {
for(int j = 0; j < clipArray.length - 1; j++) {
if(((clipArray[j].getSurname()).compareToIgnoreCase((clipArray[j+1].getSurname()))) > 0) {
Clip temp = clipArray[j];
clipArray[j] = clipArray[j+1];
clipArray[j+1] = temp;
}
}
}
for(int g = 0; g < clipArray.length; g++) {
System.out.println(clipArray[g].getSurname());
}
I am trying to print out the surnames of objects in an array.
I tested it with 2 elements in the array, and all other elements are null.
The exception happens at the if statement on the third line.