getService return null

Answered

I register the service when the project (Activity) is launched:

class MyTestStarupActivity : StartupActivity, StartupActivity.DumbAware {

override fun runActivity(project: Project) {
project.registerServiceInstance(MyTestService::class.java, MyTestService())
}
}

Then I try to get this service in CompletionContributor and get null:

class MyTestCompletionContributor : CompletionContributor() {

override fun fillCompletionVariants(parameters: CompletionParameters, result: CompletionResultSet) {
val service = parameters.editor.project?.getService(MyTestService::class.java)

Notifications.Bus.notify(
Notification(
Notifications.SYSTEM_MESSAGES_GROUP_ID,
"DEBUGGER",
"service: $service",
NotificationType.WARNING)
)
super.fillCompletionVariants(parameters, result)
}
}

Why is this happening, what am I doing wrong?

4 comments
Comment actions Permalink

Please use regular ways of registering Service as described here https://plugins.jetbrains.com/docs/intellij/plugin-services.html

1
Comment actions Permalink

I deleted my service from plugin.xml and added the @Service annotation to it. I also deleted the service registration using "registerServiceInstance". But I still get null when I want to get a service.

class MyTestStarupActivity : StartupActivity, StartupActivity.DumbAware {

override fun runActivity(project: Project) {
// There used to be a service registration here.
}
}
class MyTestCompletionContributor : CompletionContributor() {

override fun fillCompletionVariants(parameters: CompletionParameters, result: CompletionResultSet) {
val service = ApplicationManager.getApplication().getService(MyTestService::class.java)

Notifications.Bus.notify(
Notification(
Notifications.SYSTEM_MESSAGES_GROUP_ID,
"DEBUGGER",
"service: $service",
NotificationType.WARNING)
)
super.fillCompletionVariants(parameters, result)
}
}
@Service
class MyTestService {}

What could be the problem?

 
0
Comment actions Permalink

Please share full project

0
Comment actions Permalink

Thanks for the reply!

There was a mistake on my side.

0

Please sign in to leave a comment.