Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 2, 2026

Fix NullPointerException in UselessTernaryOperatorDiagnostic

The issue occurs when parsing malformed ternary operators like Возврат ?(таблица.Количество() = 1, Неопределено, ); where the third expression is missing or malformed.

Plan:

  • Explore the repository and understand the issue
  • Verify existing tests pass
  • Add null check in getBooleanToken method to handle expressions with null children
  • Create test case for the malformed ternary operator scenario
  • Run tests to verify the fix
  • Address PR review feedback: use getChildCount() and improve test
  • Use assertThatCode lambda pattern to specifically test getDiagnostics
  • Request code review
  • Run security checks
  • Run full test suite

Changes Made:

  1. Modified UselessTernaryOperatorDiagnostic.java: Use ctx.getChildCount() == 1 instead of manual null check in the getBooleanToken method (line 120)
  2. Added test file: Created UselessTernaryOperatorDiagnosticMalformed.bsl with malformed ternary operator example
  3. Improved test: Rewrote testMalformedTernaryOperatorDoesNotThrowNPE() to use assertThatCode(() -> getDiagnostics(documentContext)).doesNotThrowAnyException() pattern to specifically test that getting diagnostics doesn't throw

Root Cause:

The parser can create an ExpressionContext with null children when parsing malformed code. The original code tried to call ctx.children.size() without checking if children was null first, causing a NullPointerException.

Fix:

Use the safe getChildCount() method which handles null children internally: ctx.getChildCount() == 1

Original prompt

This section details on the original issue you should resolve

<issue_title>NullPointerException: Cannot invoke "java.util.List.size()" because "ctx.children" is null</issue_title>
<issue_description>Sentry Issue: BSL-LANGUAGE-SERVER-PY

NullPointerException: Cannot invoke "java.util.List.size()" because "ctx.children" is null
    at com.github._1c_syntax.bsl.languageserver.diagnostics.UselessTernaryOperatorDiagnostic.lambda$getBooleanToken$1(UselessTernaryOperatorDiagnostic.java:120)
    at com.github._1c_syntax.bsl.languageserver.diagnostics.UselessTernaryOperatorDiagnostic.getBooleanToken(UselessTernaryOperatorDiagnostic.java:120)
    at com.github._1c_syntax.bsl.languageserver.diagnostics.UselessTernaryOperatorDiagnostic.visitTernaryOperator(UselessTernaryOperatorDiagnostic.java:67)
    at com.github._1c_syntax.bsl.languageserver.diagnostics.UselessTernaryOperatorDiagnostic.visitTernaryOperator(UselessTernaryOperatorDiagnostic.java:42)
    at com.github._1c_syntax.bsl.parser.BSLParser$TernaryOperatorContext.accept(BSLParser.java:5332)
...
(55 additional frame(s) were not displayed)
```</issue_description>

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

<comments>
<comment_new><author>@nixel2007</author><body>
```bsl
Return ( table.Count () = 1, undefined, );

@copilot код, на котором падает.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 2, 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.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.


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

Add null check for ctx.children before accessing its size to prevent NPE when parsing malformed ternary operators

Co-authored-by: nixel2007 <[email protected]>
Copilot AI changed the title [WIP] Fix NullPointerException in UselessTernaryOperatorDiagnostic Fix NullPointerException in UselessTernaryOperatorDiagnostic for malformed ternary operators Jan 2, 2026
Copilot AI requested a review from nixel2007 January 2, 2026 17:58
- Replace ctx.children null check with ctx.getChildCount() method
- Rewrite test to properly get diagnostics list and assert with assertj

Co-authored-by: nixel2007 <[email protected]>
- Import assertThatCode from assertj
- Replace isEmpty() assertion with doesNotThrowAnyException()
- Specifically test that getDiagnostics call doesn't throw NPE

Co-authored-by: nixel2007 <[email protected]>
@nixel2007 nixel2007 marked this pull request as ready for review January 2, 2026 19:09
Copilot AI review requested due to automatic review settings January 2, 2026 19:09
@nixel2007
Copy link
Member

/buildJar

@github-actions
Copy link
Contributor

github-actions bot commented Jan 2, 2026

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

Артефакт: 5010181738

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

  • bsl-language-server-copilot-fix-nullpointerexception-issue-267ce98-exec.jar

Copilot AI requested a review from nixel2007 January 2, 2026 19:14
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 the UselessTernaryOperatorDiagnostic when parsing malformed ternary operators where the third expression is missing or malformed (e.g., Возврат ?(таблица.Количество() = 1, Неопределено, );).

Key changes:

  • Replaced unsafe direct access to ctx.children.size() with the safe ctx.getChildCount() method
  • Added a test case to verify the fix handles malformed ternary operators without throwing exceptions

Reviewed changes

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

File Description
src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UselessTernaryOperatorDiagnostic.java Changed line 120 to use getChildCount() instead of direct children.size() access to safely handle null children
src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UselessTernaryOperatorDiagnosticTest.java Added test method to verify malformed ternary operators don't cause NullPointerException
src/test/resources/diagnostics/UselessTernaryOperatorDiagnosticMalformed.bsl Added test resource file with malformed ternary operator example

The code changes are well-implemented and follow the project's conventions. The fix correctly addresses the root cause by using ANTLR's built-in getChildCount() method which safely handles null children. The test follows the established pattern in the codebase for testing exception-free behavior using assertThatCode().doesNotThrowAnyException().

@nixel2007 nixel2007 merged commit 9c1f478 into develop Jan 2, 2026
45 checks passed
@nixel2007 nixel2007 deleted the copilot/fix-nullpointerexception-issue branch January 2, 2026 19:25
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.

NullPointerException: Cannot invoke "java.util.List.size()" because "ctx.children" is null

2 participants