Bhanuka Withana
Bhanuka Withana

Reputation: 190

JDT SearchEngine throws a NullPointerException

I'm trying to use JDT SearchEngine to find references to a given object. But I'm getting a "NullPointerException" while invoking the "search" method of org.eclipse.jdt.core.search.SearchEngine.

Following is the error trace:

java.lang.NullPointerException at org.eclipse.jdt.internal.core.search.BasicSearchEngine.findMatches(BasicSearchEngine.java:214) at org.eclipse.jdt.internal.core.search.BasicSearchEngine.search(BasicSearchEngine.java:515) at org.eclipse.jdt.core.search.SearchEngine.search(SearchEngine.java:582)

And following is the method I'm using to perform search:

private static void search(String elementName) { //elementName -> a method Name
        try {
            SearchPattern pattern = SearchPattern.createPattern(elementName, IJavaSearchConstants.METHOD,
                    IJavaSearchConstants.REFERENCES, SearchPattern.R_PATTERN_MATCH);

            IJavaSearchScope scope = SearchEngine.createWorkspaceScope();

            SearchRequestor requestor = new SearchRequestor() {
                @Override
                public void acceptSearchMatch(SearchMatch match) {
                    System.out.println("Element - " + match.getElement());
                }
            };

            SearchEngine searchEngine = new SearchEngine();
            SearchParticipant[] searchParticipants = new SearchParticipant[] { SearchEngine
                    .getDefaultSearchParticipant() };
            searchEngine.search(pattern, searchParticipants, scope, requestor, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Refer the "Variables" window of the following snapshot to check the values of the arguments passing to the "searchEngine.search()":

enter image description here

I think the the issue is because of the value of "scope" [Highlighted in 'BLACK' above]. Which means "SearchEngine.createWorkspaceScope()" doesn't return expected values in this case.

NOTE: Please note that this is a part of my program which runs as a stand-alone java program (not an eclipse plugin) using JDT APIs to parse a given source code (using JDT-AST).

Isn't it possible to use JDT SearchEngine in such case (non eclipse plugin program), or is this issue due to some other reason? Really appreciate your answer on this.

Upvotes: 3

Views: 567

Answers (1)

Andrew Eisenberg
Andrew Eisenberg

Reputation: 28757

No. You cannot use the search engine without openning a workspace. The reason is that the SearchEngine relies on the eclipse filesystem abstraction (IResource, IFile, IFolder, etc.). This is only available when the workspace is open.

Upvotes: 3

Related Questions