Null pointer exception while accessing service methods in Liferay 7.3Null pointer exception while accessing service methods in Liferay 7.3https://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=1204249312024-03-29T05:28:53Z2024-03-29T05:28:53ZNull pointer exception while accessing service methods in Liferay 7.3Sampada Karanjithttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1204249302020-11-11T08:50:02Z2020-11-11T08:50:02Z<html><head></head><body><p>Hi,</p>
<p>We are trying to migrate the existing portlets from Liferay 7.1 to
Liferay 7.3(liferay/dxp:7.3.10-ga1 version).</p>
<p>After adding necessary <a>Dependency Management</a> related changes,
building service and docker deploy, all portlets are shown in an
Active State. However, we are not able to access any of the
service methods inside our portlets. </p>
<p>After conducting some research on this issue, it seems that the
default way of injecting services is now done by using Declarative
Service approach. <a href="https://sourceforge.net/p/lportal/news/2019/07/important-72-service-builder-changes/">Important
Service Builder Changes</a> points out that, we can still use old
way of service injections, provided that we specify
dependency-injector as "Spring" in the service.xml file. </p>
<p>Steps we tried:</p>
<p>1) Modify existing Service.xml as following</p>
<pre><code class="language-java">&lt;?xml version="1.0"?&gt;&lt;!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.3.0//EN"
"http://www.liferay.com/dtd/liferay-service-builder_7_3_0.dtd"&gt;
&lt;service-builder package-path="sample.liferay" dependency-injector="spring"&gt;
&lt;entity local-service="true" name="Foo" remote-service="true" uuid="true"&gt;&lt;/entity&gt;
&lt;/service&gt;</code></pre>
<p>2) Build Service</p>
<p>3) Access the service in our portlet as:</p>
<pre><code class="language-java">
FooLocalServiceUtil.createFoo(1);
FooLocaServiceUtil.getFoosCount();</code></pre>
<p>4) Docker Deploy</p>
<p>After the respective Bundle Started message is displayed, we tried to
deploy the portlet in our web page and then we always encounter
"<em>Portlet is temporarily not available</em>".</p>
<p>Upon checking on logs, we see a Null Pointer Exception at
<em>getService()</em> of FooLocalServiceUtil methods . </p>
<p>We also tried to use @Reference annotation</p>
<pre><code class="language-java">@Reference
private FooLocalService fooLocalService;
fooLocalService.getFoosCount();</code></pre>
<p>Result: Same exception</p>
<p>Further, we also tried by creating a sample module and generating
service builder in Liferay 7.3. By default the service.xml contain the
dependency injector as DS and necessary annotations in bnd.bnd file.
After building the service, we tried to access one of the methods from
the portlet using @Reference annotation.</p>
<pre><code class="language-java">@Reference
private FooNewLocalService fooNewLocalService;
fooNewLocalService.createFooNew(1);
</code></pre>
<p>However, we face the same null pointer exception while accessing
service methods from the portlet.</p>
<p>Initially it seemed that the problem might be the dependency
injection technique probably not being backward compatible. But now
as, the new approach also results in the same exception, we think that
probably the problem might reside somewhere else. It could be that we
are missing a small but critical step here. </p>
<p>Please let us know, if you have any ideas or hints regarding the
issue. </p>
<p>Thank you very much</p></body></h