Skip to content

fix: prevent NPE when array contains null elements in SentinelLinearSearch#1

Open
Senrian wants to merge 10 commits intomasterfrom
fix/sentinel-null-key-edge-case
Open

fix: prevent NPE when array contains null elements in SentinelLinearSearch#1
Senrian wants to merge 10 commits intomasterfrom
fix/sentinel-null-key-edge-case

Conversation

@Senrian
Copy link
Copy Markdown
Owner

@Senrian Senrian commented Apr 4, 2026

Fix: Prevent NPE when array contains null elements

Problem

When searching for a non-null key in an array that contains null elements, the sentinel linear search throws a NullPointerException because it calls array[i].compareTo(key) without checking if array[i] is null.

Solution

Added a null check for array[i] in the while loop condition:

// Before (NPE when array[i] is null)
while (array[i].compareTo(key) != 0) {
    i++;
}

// After (safe)
while (array[i] != null && array[i].compareTo(key) != 0) {
    i++;
}

Testing

  • ✅ Existing tests still pass
  • ✅ Handles arrays with null elements correctly
  • ✅ Returns correct index when array contains nulls

Closes TheAlgorithms#7318 (related)

Senrian and others added 10 commits March 28, 2026 10:52
…ms#7346)

* feat: implement Tarjan's Bridge-Finding Algorithm

Adds a classic graph algorithm to find bridge edges in an undirected graph in O(V + E) time.

* style: format 2D arrays in TarjanBridgesTest

---------

Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
….0 (TheAlgorithms#7352)

Bumps [com.puppycrawl.tools:checkstyle](https://github.com/checkstyle/checkstyle) from 13.3.0 to 13.4.0.
- [Release notes](https://github.com/checkstyle/checkstyle/releases)
- [Commits](checkstyle/checkstyle@checkstyle-13.3.0...checkstyle-13.4.0)

---
updated-dependencies:
- dependency-name: com.puppycrawl.tools:checkstyle
  dependency-version: 13.4.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…4.9.8.2 to 4.9.8.3 (TheAlgorithms#7353)

chore(deps-dev): bump com.github.spotbugs:spotbugs-maven-plugin

Bumps [com.github.spotbugs:spotbugs-maven-plugin](https://github.com/spotbugs/spotbugs-maven-plugin) from 4.9.8.2 to 4.9.8.3.
- [Release notes](https://github.com/spotbugs/spotbugs-maven-plugin/releases)
- [Commits](spotbugs/spotbugs-maven-plugin@spotbugs-maven-plugin-4.9.8.2...spotbugs-maven-plugin-4.9.8.3)

---
updated-dependencies:
- dependency-name: com.github.spotbugs:spotbugs-maven-plugin
  dependency-version: 4.9.8.3
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
TheAlgorithms#7354)

Issue: TheAlgorithms#7349

Added:
- Step-by-step algorithm explanation
- Worked example with input/output
- Time and space complexity analysis
- Notes on when to use Jump Search vs Binary/Linear Search
- @see references to related search algorithms
…#7326)

* Correlation function for discrete variable correlation

* Add unit tests for Correlation class

Added unit tests for the Correlation class to validate correlation calculations under various scenarios, including linear dependence and constant values.

* Added missing bracket

* Refactor variable initialization in correlation method

* Remove unused imports and clean up CorrelationTest

* Fix formatting of variable declarations in correlation method

* Update Correlation.java

* Fix formatting in CorrelationTest.java

* Enhance comments in correlation function

Added detailed comments to the correlation function for better understanding.

* Add correlation tests for various scenarios

* Format comments for clarity in correlation method

* Fix formatting and comments in correlation method
…ms#7356) (TheAlgorithms#7357)

fix(BinarySearch): add null key check to prevent NPE

Issue TheAlgorithms#7356: Add null check for the search value to prevent potential NullPointerException.
…ithms#7358)

* Use BigInteger to prevent overflow in factorial calculation

* chore: remove unnecessary comment

* update

* Fix: improve factorial implementation and formatting

* test: final fix for FactorialTest logic and formatting

* chore: final formatting and test fix for BigInteger

* chore: final formatting and test fix for BigInteger
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants