Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shield against NPE in VSCode #7466

Merged
merged 1 commit into from
Jun 27, 2024

Conversation

jtulach
Copy link
Contributor

@jtulach jtulach commented Jun 13, 2024

We are experiencing NPE when running Enso VSCode extension:

Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" 
because the return value of 
"org.netbeans.spi.lsp.ErrorProvider.computeErrors(org.netbeans.spi.lsp.ErrorProvider$Context)" 
is null
	at org.netbeans.modules.java.lsp.server.protocol.TextDocumentServiceImpl.lambda$computeDiags$46(TextDocumentServiceImpl.java:2027)
	at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:273)

after some debugging I found that the code in TextDocumentServiceImpl added by Dušan four months ago and the code written by Sváťa two years ago disagree on whether a null is valid return type or not which leads to NullPointerException.

This PR is fixing that by checking for null on the caller side.

@jtulach jtulach added the VSCode Extension [ci] enable VSCode Extension tests label Jun 13, 2024
@jtulach jtulach requested review from dbalek and sdedic June 13, 2024 15:11
@jtulach jtulach self-assigned this Jun 13, 2024
Comment on lines +2004 to +2010
errors = errorProviders.stream().flatMap(provider -> {
List<? extends org.netbeans.api.lsp.Diagnostic> errorsOrNull = provider.computeErrors(context);
if (errorsOrNull == null) {
errorsOrNull = Collections.emptyList();
}
return errorsOrNull.stream();
}).collect(Collectors.toList());
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I'd encode the desired behaviour in the stream operation directly but what you have will work too

Suggested change
errors = errorProviders.stream().flatMap(provider -> {
List<? extends org.netbeans.api.lsp.Diagnostic> errorsOrNull = provider.computeErrors(context);
if (errorsOrNull == null) {
errorsOrNull = Collections.emptyList();
}
return errorsOrNull.stream();
}).collect(Collectors.toList());
errors = errorProviders.stream()
.map(provider -> provider.computeErrors(context))
.filter(Object::nonNull)
.flatMap(List::stream)
.collect(Collectors.toList());

@jtulach jtulach merged commit 3829261 into apache:master Jun 27, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VSCode Extension [ci] enable VSCode Extension tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants