Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 6, 2026

Plan to Fix NullPointerException in DeletingCollectionItemDiagnostic

Issue

NullPointerException occurs when typing incomplete forEach statements like "Для каждого " (For each) because ctx.expression() returns null when the expression part is missing.

Checklist

  • Add null check for ctx.expression() in visitForEachStatement method
  • Create test case for incomplete forEach statement
  • Run existing tests to ensure no regression
  • Verify the fix handles the incomplete statement gracefully
  • Code review completed with no issues
  • Security scan completed with no issues
  • Address review feedback: remove trailing whitespace and use assertThatCode pattern

Summary

Successfully fixed the NullPointerException by adding a null check before accessing ctx.expression(). The diagnostic now gracefully handles incomplete forEach statements during code editing. All tests pass and no security vulnerabilities were introduced.

Review Feedback Addressed

  • Removed trailing whitespace control character (\s) from test string
  • Changed test to use assertThatCode(() -> getDiagnostics(documentContext)).doesNotThrowAnyException() pattern to explicitly verify no exception is thrown
Original prompt

This section details on the original issue you should resolve

<issue_title>NullPointerException: Cannot invoke "com.github._1c_syntax.bsl.parser.BSLParser$ExpressionContext.getText()" because the return value of "com.github._1c_syntax.bsl.parser.BSLParser$ForEachStatementContext.expression()" is null</issue_title>
<issue_description>При наборе "Для каждого "

Sentry Issue: BSL-LANGUAGE-SERVER-4F

NullPointerException: Cannot invoke "com.github._1c_syntax.bsl.parser.BSLParser$ExpressionContext.getText()" because the return value of "com.github._1c_syntax.bsl.parser.BSLParser$ForEachStatementContext.expression()" is null
    at com.github._1c_syntax.bsl.languageserver.diagnostics.DeletingCollectionItemDiagnostic.visitForEachStatement(DeletingCollectionItemDiagnostic.java:70)
    at com.github._1c_syntax.bsl.languageserver.diagnostics.DeletingCollectionItemDiagnostic.visitForEachStatement(DeletingCollectionItemDiagnostic.java:39)
    at com.github._1c_syntax.bsl.parser.BSLParser$ForEachStatementContext.accept(BSLParser.java:4393)
    at com.github._1c_syntax.bsl.parser.BSLParserBaseVisitor.visitCompoundStatement(BSLParserBaseVisitor.java:750)
    at com.github._1c_syntax.bsl.parser.BSLParser$CompoundStatementContext.accept(BSLParser.java:8206)
...
(40 additional frame(s) were not displayed)
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 6, 2026

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI changed the title [WIP] Fix NullPointerException in ForEachStatement handling Fix NullPointerException in DeletingCollectionItemDiagnostic for incomplete forEach statements Jan 6, 2026
Copilot AI requested a review from nixel2007 January 6, 2026 07:40
Copilot AI requested a review from nixel2007 January 6, 2026 07:56
@nixel2007 nixel2007 marked this pull request as ready for review January 6, 2026 08:00
Copilot AI review requested due to automatic review settings January 6, 2026 08:00
@nixel2007 nixel2007 enabled auto-merge January 6, 2026 08:00
@nixel2007
Copy link
Member

/buildJar

@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

✅ Собраны JAR-файлы для этого PR по команде /buildJar.

Артефакт: 5034073540

Файлы внутри:

  • bsl-language-server-copilot-fix-null-pointer-exception-68c53c9-exec.jar

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a NullPointerException that occurs in DeletingCollectionItemDiagnostic when developers type incomplete forEach statements like "Для Каждого" (For each) without an expression. The issue happened because the code attempted to call getText() on a null expression context.

Key Changes:

  • Added null check for ctx.expression() before accessing it
  • Added regression test to verify incomplete forEach statements don't throw exceptions

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/DeletingCollectionItemDiagnostic.java Added null check and early return when forEach expression is null to prevent NullPointerException
src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/DeletingCollectionItemDiagnosticTest.java Added regression test using assertThatCode pattern to verify incomplete forEach statements are handled gracefully

@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

Test Results

 2 808 files  ±0   2 808 suites  ±0   51m 24s ⏱️ +31s
 1 157 tests +1   1 157 ✅ +1  0 💤 ±0  0 ❌ ±0 
10 413 runs  +9  10 413 ✅ +9  0 💤 ±0  0 ❌ ±0 

Results for commit 68c53c9. ± Comparison against base commit 6acf373.

This pull request removes 8 and adds 5 tests. Note that renamed tests count towards both.
#, count=1
, count=0
, count=1
A, count=1
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [2] content=
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [6] content=
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [7] content=#
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [8] content=
com.github._1c_syntax.bsl.languageserver.diagnostics.DeletingCollectionItemDiagnosticTest ‑ testIncompleteForEachStatement()
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [2] content=
, count=0
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [6] content=
A, count=1
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [7] content=#
, count=1
com.github._1c_syntax.bsl.languageserver.diagnostics.MetadataObjectNameLengthDiagnosticTest ‑ [8] content=
#, count=1

♻️ This comment has been updated with latest results.

@nixel2007 nixel2007 merged commit 8eb4a2f into develop Jan 6, 2026
57 of 58 checks passed
@nixel2007 nixel2007 deleted the copilot/fix-null-pointer-exception branch January 6, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants