留言板

LocalService Util getservice method returns null

Kiril James Mangubat,修改在6 年前。

LocalService Util getservice method returns null

New Member 帖子: 4 加入日期: 17-9-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
David H Nebinger,修改在6 年前。

RE: LocalService Util getservice method returns null

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
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
Robert Chen,修改在6 年前。

RE: LocalService Util getservice method returns null

New Member 帖子: 19 加入日期: 12-1-24 最近的帖子
Thank you, @David H Nebinger! Your posts are very helpful!