Initialize and use object using @Reference of Liferay's Service Classes

Vishal Shah, modified 7 Years ago. Junior Member Posts: 33 Join Date: 2/1/12 Recent Posts

Previously, using Liferay's Default Service Builder classes using *LocalServiceUtil and *ServiceUtil in custom portlet. Now I want to initalise and use via @Reference.

 

I am trying for this another way but object is not initalise and getting NULL.

 

protected KBArticleService kbArticleService;
protected KBCommentLocalService kbCommentLocalService;

 

@Reference(unbind = "-")
protected void setKBArticleService(KBArticleService kbArticleService) {
        this.kbArticleService = kbArticleService;
}

@Reference(unbind = "-")
 protected void setKBCommentLocalService(
        KBCommentLocalService kbCommentLocalService) {

        this.kbCommentLocalService = kbCommentLocalService;
 }

 

My custome method implemented below code :

 

kbArticle = kbArticleService.addKBArticle(
                portletId, parentResourceClassNameId, parentResourcePrimKey,
                title, urlTitle, content, description, sourceURL, sections,
                selectedFileNames, serviceContext);
 

But everytime getting kbArticleService not initalise and getting NULLpointer exception.

 

I think any missing from my side. I added below line in gradle :

compileOnly group: 'com.liferay', name: 'com.liferay.knowledge.base.api', version: '5.0.11'

compileOnly group: 'com.liferay', name: 'com.liferay.knowledge.base.service', version: '2.0.20'

thumbnail
Olaf Kock, modified 7 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Vishal Shah:

Previously, using Liferay's Default Service Builder classes using *LocalServiceUtil and *ServiceUtil in custom portlet. Now I want to initalise and use via @Reference.

 

I am trying for this another way but object is not initalise and getting NULL.

...

I think any missing from my side. I added below line in gradle :

compileOnly group: 'com.liferay', name: 'com.liferay.knowledge.base.api', version: '5.0.11'

compileOnly group: 'com.liferay', name: 'com.liferay.knowledge.base.service', version: '2.0.20'

I'd expect that you don't have dependency problems, e.g. you can't fix it through gradle. But you probably are using the @Reference in a class that's not a @Component. @Reference needs to be within a @Component to be initialized on deployment. Then, if your component doesn't deploy, you'll need to worry about the dependencies.