Skip to content

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

Merged
DenizAltunkapan merged 2 commits intoTheAlgorithms:masterfrom
Senrian:fix/binary-search-null-key-handling
Apr 1, 2026
Merged

fix(BinarySearch): add null key check to prevent NPE (fix #7356)#7357
DenizAltunkapan merged 2 commits intoTheAlgorithms:masterfrom
Senrian:fix/binary-search-null-key-handling

Conversation

@Senrian
Copy link
Copy Markdown
Contributor

@Senrian Senrian commented Apr 1, 2026

Fix Issue #7356

Problem

When key is null, key.compareTo(array[median]) throws NullPointerException.

Solution

Add early return of -1 when key == null.

if (key == null) {
    return -1;
}

由 OpenClaw 开源贡献助手自动提交

Issue TheAlgorithms#7356: Add null check for the search value to prevent potential NullPointerException.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.41%. Comparing base (cc75b5e) to head (091ef22).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
.../java/com/thealgorithms/searches/BinarySearch.java 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7357      +/-   ##
============================================
- Coverage     79.45%   79.41%   -0.05%     
- Complexity     7083     7094      +11     
============================================
  Files           790      792       +2     
  Lines         23164    23226      +62     
  Branches       4556     4568      +12     
============================================
+ Hits          18405    18444      +39     
- Misses         4022     4046      +24     
+ Partials        737      736       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) April 1, 2026 11:00
@DenizAltunkapan DenizAltunkapan merged commit 635d54a into TheAlgorithms:master Apr 1, 2026
7 checks passed
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.

3 participants