掲示板

LocalService Util getservice method returns null

6年前 に Kiril James Mangubat によって更新されました。

LocalService Util getservice method returns null

New Member 投稿: 4 参加年月日: 17/09/20 最新の投稿
Currently, I'm developing a web application using Liferay 7 GA3.
In my application, I have custom table defined in service.xml of service-builder.
During development, I am using a local database and no problems/errors found.
When I use a database in remote server, the following errors occurred.

Caused by: java.lang.NullPointerException
at jp.ubsecure.portal.jubjub.portlet.service.ProjectLocalServiceUtil.getProjects(ProjectLocalServiceUtil.java:250)
at jp.ubsecure.portal.jubjub.portlet.controller.ControllerHelper.getProjects(ControllerHelper.java:336)
at org.apache.jsp.html.android.projectmanagement.project_005flist_jsp._jspService(project_005flist_jsp:522)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
... 225 more

at line 250 of ProjectLocalServiceUtil.java

public static java.util.List<java.lang.object> getProjects(
		java.util.Map<java.lang.string, java.lang.object> searchedProject,
		com.liferay.portal.kernel.model.User user, int type, int start)
		throws com.liferay.portal.kernel.exception.PortalException {
		return getService().getProjects(searchedProject, user, type, start);  //line 250
}

public static ProjectLocalService getService() {
	return _serviceTracker.getService();
}
</java.lang.string,></java.lang.object>


I also tried to resolved the issue using the solution suggested here as I thought it is similar to mine but the solution doesn't work.
thumbnail
6年前 に David H Nebinger によって更新されました。

RE: LocalService Util getservice method returns null

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
I'm at the point where I never use the XxxLocalServiceUtil classes, not even in my JSPs.

Instead in my portlet class, I'll use an @Reference to inject the XxxLocalService into a private member field.

Then I override the render() method and set request attributes with the service, i.e.:

renderRequest.setAttribute("xxxLocalService", _xxxLocalService);


On the JSP side, in init.jsp I reverse the process:

&lt;%
	XxxLocalService xxxLocalService = (XxxLocalService) renderRequest.getAttribute("XxxLocalService");
%&gt;


Since all of my JSPs include init.jsp, they have visibility on the xxxLocalService variable and can access it directly.

Yes this can get cumbersome. But here's the thing - if you are using the XxxLocalServiceUtil class, you have no idea if the service jar has been deployed and is started (XxxLocalServiceUtil uses a service tracker so it can start even when an implementation is not available), leading often to NPEs of this nature.

With this implementation, the @Reference forces OSGi to resolve the reference and inject it. If there is no reference available, the bundle won't start, but that's an immediate condition that I'm notified of and know how to resolve, it's not hidden under the covers just waiting to expose itself.







Come meet me at Devcon 2017 or 2017 LSNA!
thumbnail
6年前 に Robert Chen によって更新されました。

RE: LocalService Util getservice method returns null

New Member 投稿: 19 参加年月日: 12/01/24 最新の投稿
Thank you, @David H Nebinger! Your posts are very helpful!