Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
-*LocalServiceUtil.getService returns null.
Following is the auto-generated code by the service builder inside the UserLocalServiceUtil class.
When I invoke UserLocalServiceUtil.getService(), it returns null.
public static UserLocalService getService() {
return _serviceTracker.getService();
}
private static ServiceTracker<userlocalservice, userlocalservice> _serviceTracker =
ServiceTrackerFactory.open(UserLocalService.class);</userlocalservice,>When I invoke UserLocalServiceUtil.getService(), it returns null.
It is why you should not be using any static service util classes, local or remote.
Getting a null back can happen for any number of reasons; service is not available (so service tracker doesn't have an instance to return), service has not started yet (waiting on other dependency resolutions), etc.
You should only be @Reference injecting the services you need. That way your component will not start until the service is ready and injected and you won't have to deal with NPEs.
And sure, your component might not start if the service never becomes available, but that is not the fault of your component, it is the fault of your service. You don't want your component to start unless the service is ready.
Getting a null back can happen for any number of reasons; service is not available (so service tracker doesn't have an instance to return), service has not started yet (waiting on other dependency resolutions), etc.
You should only be @Reference injecting the services you need. That way your component will not start until the service is ready and injected and you won't have to deal with NPEs.
And sure, your component might not start if the service never becomes available, but that is not the fault of your component, it is the fault of your service. You don't want your component to start unless the service is ready.
Thanks David. I'll use the @Reference to inject the service.
David H Nebinger:
It is why you should not be using any static service util classes, local or remote.
Getting a null back can happen for any number of reasons; service is not available (so service tracker doesn't have an instance to return), service has not started yet (waiting on other dependency resolutions), etc.
You should only be @Reference injecting the services you need. That way your component will not start until the service is ready and injected and you won't have to deal with NPEs.
And sure, your component might not start if the service never becomes available, but that is not the fault of your component, it is the fault of your service. You don't want your component to start unless the service is ready.