Commit d44b608
committed
fix: prevent NPE when array contains null elements
When searching for a non-null key in an array that contains null elements,
the sentinel linear search would throw a NullPointerException because it
called array[i].compareTo(key) without checking if array[i] is null.
Added null check for array[i] in the while loop condition to prevent NPE
and return the correct index when array elements themselves are null.
Issue: TheAlgorithms#7318 (related)1 parent 9729e56 commit d44b608
File tree
1 file changed
+2
-1
lines changed- src/main/java/com/thealgorithms/searches
1 file changed
+2
-1
lines changedLines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| |||
0 commit comments