Skip to content

NPE occur when expanding the default package #611

@mccreery

Description

@mccreery

I have a very simple Gradle (5.1.1) project with JUnit 4 tests configured in my build.gradle. The resulting .classpath file used by Eclipse JDT contains the following entries:

Details

Eclipse .classpath entries
<classpathentry kind="src" output="bin/main" path="src/main/java">
    <attributes>
        <attribute name="gradle_scope" value="main"/>
        <attribute name="gradle_used_by_scope" value="main,test"/>
    </attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
    <attributes>
        <attribute name="gradle_scope" value="test"/>
        <attribute name="gradle_used_by_scope" value="test"/>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>

(I added the "test" attribute to the test entry manually, as per #470.)

Everything is compiled correctly and the VS Code Problems view is empty. However, when I try to load my tests (just one class, src/test/java/Tests.java) into the test explorer, I reach a NullPointerException from this line:

NPE Stacktrace
java.lang.NullPointerException
	at com.microsoft.java.test.plugin.util.TestSearchUtils.searchInPackage(TestSearchUtils.java:294)
	at com.microsoft.java.test.plugin.util.TestSearchUtils.searchTestItems(TestSearchUtils.java:133)
	at com.microsoft.java.test.plugin.handler.TestDelegateCommandHandler.executeCommand(TestDelegateCommandHandler.java:45)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:152)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:142)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$4(JDTLanguageServer.java:451)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$26(JDTLanguageServer.java:793)
	at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
	at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
	at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:443)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

It seems like resolvePackage can and does return null, but this plugin does not check for null.

Code

src/test/java/Tests.java
import org.junit.Assert;
import org.junit.Test;

public class Tests {
    @Test
    public void testA() {
        Assert.assertTrue(true);
    }
}
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

Reproduction

Just create a Gradle 5.1.1 (should still happen on other versions) project with the above code and populate the test explorer, then try to expand the empty entry under the root project. Mine looks like this:
Test Explorer view

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions