Trying to bubble sort an array of 2 objects, the comments are what the 2 positions (0 and 1) contain. It does actually populate the array. When I'm comparing ownerArray[j] and ownerArray [j+1] I get a null pointer exception, because I'm referring to a null value because the array isn't that large. Any clue how to fix this while still being able to refer to the next position of the array?
public void sortOwners() {
try {
model = (DefaultTableModel) SortedOwners.jTable1.getModel();
model.setRowCount(0);
populateOwners();
size2 = ownerArray.length;
// ownerArray[0] = 9900000000000 Reenen Muller xxxxxx@xxxxx.co.za
// ownerArray[1] = 8800000000000 John Doe yyyyyy@yyyyy.co.za
for (int i = 0; i < size2; i++) {
for (int j = 1; j < size2 - i; j++) {
if ((ownerArray[j].getFirstName()).compareTo(ownerArray[j + 1].getFirstName()) < 0) {
Owner temp3 = ownerArray[j];
ownerArray[j] = ownerArray[j + 1];
ownerArray[j + 1] = temp3;
}
}
}
viewAllOwners();
} catch (Exception e) {
e.printStackTrace();
}