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

Add null check for 'fCollapsedTypes' variable to avoid NPE when invoking completion in DEBUG CONSOLE #3086

Merged

Conversation

testforstephen
Copy link
Contributor

With the pre-release version of Java language support extension installed, triggering method completion on DEBUG CONSOLE will throw NPE exception as below.

Cannot invoke "java.util.Map.getOrDefault(Object, Object)" because "this.fCollapsedTypes" is null
!STACK 0
java.lang.NullPointerException: Cannot invoke "java.util.Map.getOrDefault(Object, Object)" because "this.fCollapsedTypes" is null
	at org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalDescriptionProvider.createMethodProposalLabel(CompletionProposalDescriptionProvider.java:334)
	at org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalDescriptionProvider.updateDescription(CompletionProposalDescriptionProvider.java:745)
	at com.microsoft.java.debug.plugin.internal.CompletionProposalRequestor.toCompletionItem(CompletionProposalRequestor.java:165)
	at com.microsoft.java.debug.plugin.internal.CompletionProposalRequestor.getCompletionItems(CompletionProposalRequestor.java:147)
	at com.microsoft.java.debug.plugin.internal.CompletionsProvider.codeComplete(CompletionsProvider.java:75)
	at com.microsoft.java.debug.core.adapter.handler.CompletionsHandler.lambda$handle$0(CompletionsHandler.java:68)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(Unknown Source)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)

The issue occurs because the Java Debugger uses the old constructor CompletionProposalDescriptionProvider(CompletionContext context) to initialize the DescriptionProvider. This constructor does not initialize the new field fCollapsedTypes, resulting in a NullPointerException when fCollapsedTypes is later referenced. It is important to ensure that when introducing new fields in the code, they do not break any existing usage.

Comment on lines 73 to 79
fCollapsedTypes = new HashMap<>();
}

public CompletionProposalDescriptionProvider(CompletionContext context) {
super();
fContext = context;
fCollapsedTypes = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

if the maps are read-only, then Collections.emptyMap() would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I change the approach to add null check to avoid NPE, that's a more straightforward fix.

@testforstephen testforstephen changed the title Assign an init value to 'fCollapsedTypes'variable to avoid NPE when invoking completion on DEBUG CONSOLE Add null check for 'fCollapsedTypes' variable to avoid NPE when invoking completion in DEBUG CONSOLE Mar 5, 2024
@testforstephen testforstephen merged commit 4b2b504 into eclipse-jdtls:master Mar 6, 2024
7 checks passed
@testforstephen testforstephen deleted the jinbo_completion branch March 6, 2024 02:05
@rgrunber rgrunber added this to the Mid March 2024 milestone Mar 6, 2024
@rgrunber rgrunber added the bug label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants