Foros de discusión

java.lang.NullPointerException

Abhishek Jain, modificado hace 7 años.

java.lang.NullPointerException

New Member Mensaje: 1 Fecha de incorporación: 29/11/16 Mensajes recientes
I made an entity CustomUser and built the services. CustomUserLocalServiceUtil is generated.I made a custom method in CustomUserLocalServiceImpl and am calling it via CustomUserLocalServiceUtil but it is giving me the above mentioned error. I debugged the code and found that getService() method of CustomUserLocalServiceUtil is returning null. Can you tell me why is it returning null and what to do to sort this error. Here is the comple stacktrace:-

java.lang.NullPointerException
at com.example.service.CustomUserLocalServiceUtil.findByname(CustomUserLocalServiceUtil.java:218)
at com.example.portlet.Demo9mvcportletPortlet.render(Demo9mvcportletPortlet.java:32)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:103)
at com.liferay.portlet.ScriptDataPortletFilter.doFilter(ScriptDataPortletFilter.java:57)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:100)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:64)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:105)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at com.liferay.portal.osgi.web.servlet.context.helper.internal.ServletContextHelperRegistrationImpl$PortletServletWrapper.service(ServletContextHelperRegistrationImpl.java:427)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:153)
at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:50)
at com.liferay.portal.osgi.web.servlet.context.helper.internal.ServletContextHelperRegistrationImpl$RestrictPortletServletRequestFilter.doFilter(ServletContextHelperRegistrationImpl.java:447)
at org.eclipse.equinox.http.servlet.internal.registration.FilterRegistration.doFilter(FilterRegistration.java:121)
at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:45)
at org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:70)
at org.eclipse.equinox.http.servlet.internal.context.DispatchTargets.doDispatch(DispatchTargets.java:119)
at org.eclipse.equinox.http.servlet.internal.servlet.RequestDispatcherAdaptor.include(RequestDispatcherAdaptor.java:48)
at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:529)
at com.liferay.portlet.InvokerPortletImpl.invokeRender(InvokerPortletImpl.java:604)
Please help..thanx in advance..
thumbnail
David H Nebinger, modificado hace 7 años.

RE: java.lang.NullPointerException

Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
You should not be using the Util class directly, you should use @Reference annotations and let OSGi inject the dependency.

Here's the crux of the issue. OSGi will start bundles and, once all dependencies have been injected, those bundles will be ready in OSGi. When you use the Util classes, as an external module it will need to be started by OSGi and therefore the dependencies will be injected correctly. But when you access it via the jar, you're bypassing the OSGi framework and can call w/o OSGi injecting the reference, thus never setting things up correctly.

Use the OSGi @Reference annotation. Allow OSGi to inject the reference before your portlet starts. Skip the legacy stuff.
thumbnail
Aravinth Kumar, modificado hace 4 años.

RE: java.lang.NullPointerException

Regular Member Mensajes: 152 Fecha de incorporación: 26/06/13 Mensajes recientes
Hi David,I m using liferay 7.2.I have created some osgi service as a separate module and consuming it in both osgi module and spring mvc portlet.
I m able to access the service in osgi module through @Reference annotation and in Spring mvc controller through BundleContext and ServiceTracker.
Now I m trying to access the service through @ServiceReference in spring mvc portlet but its throwing null pointer exception.
Can you please guide me. Thanks in Advance.Regards,Aravinth
thumbnail
David H Nebinger, modificado hace 4 años.

RE: java.lang.NullPointerException

Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
Yeah, you can't really @ServiceReference in an OSGi component.

I usually follow ServiceBuilder's pattern and create a static util class with the service tracker and then use it in my spring code. Not necessarily a "pretty" way of doing it, but it does work...
thumbnail
Aravinth Kumar, modificado hace 4 años.

RE: java.lang.NullPointerException

Regular Member Mensajes: 152 Fecha de incorporación: 26/06/13 Mensajes recientes
Thanks for your response. 
Regards,Aravinth