error liferay: mvc portlet don't connect to the service builder , java.lang.NullPointerException

602 views Asked by At

i m a bigenner at liferay , i followed a tutoriel to Connect Service Builder to External Database in Liferay using MAVEN . i add my external database like follow:

jdbc.ext.url=jdbc:postgresql://localhost:8085/MyDB2
jdbc.ext.username=postgres
jdbc.ext.password=myPassWord

then I create my service builder which is building well

<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.4.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_4_0.dtd">

<service-builder dependency-injector="spring" package-path="ntlink.db">
   <namespace>ntlink</namespace>
   <entity name="Candidat" local-service="true" table="candidats" data-source="candidat">
       <column name="id_candidat" type="int" primary="true"></column>
       <column name="nom_candidat" type="String"></column>
       <column name="prenom_candidat" type="String"></column>
       <column name="email_candidat" type="String"></column>
       <column name="motdepass_cnandidat" type="String"></column>
   </entity>
</service-builder>

then now I have my API.jar and service.jar the two of them are in my ./m2 folder then I create my MVC portlet :

@SuppressWarnings("unused")
@Component(
   immediate = true,
   property = { 
       "com.liferay.portlet.display-category=category.sample",
       "com.liferay.portlet.header-portlet-css=/css/main.css",
       "com.liferay.portlet.instanceable=true",
       "javax.portlet.display-name=Candidat",
       "javax.portlet.init-param.template-path=/",
       "javax.portlet.init-param.view-template=/view.jsp",
       "javax.portlet.name=" + CandidatPortletKeys.CANDIDAT,
       "javax.portlet.resource-bundle=content.Language",
       "javax.portlet.security-role-ref=power-user,user"
   },
   service = Portlet.class
) 
public class CandidatPortlet extends MVCPortlet { 
   
   
   @Reference
   private volatile CandidatLocalService candidatLocalService;
   
  @Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
       throws java.io.IOException, PortletException {
      try {
          System.out.println("hello");
       System.out.println("values===>"+ candidatLocalService.getCandidat(1).getEmail_candidat());
   } catch (PortalException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   }
   super.doView(renderRequest, renderResponse);
}
 
}

with the annotation @Reference my portlet don't show up in the Widgets and in the GOGO shell i found this error

Config Props: (11 entries)
 com.liferay.portlet.display-category<String> = category.sample
 com.liferay.portlet.header-portlet-css<String> = /css/main.css
 com.liferay.portlet.instanceable<String> = true
 component.id<Long> = 9479
 component.name<String> = candidat.portlet.CandidatPortlet
 javax.portlet.display-name<String> = Candidat
 javax.portlet.init-param.template-path<String> = /
 javax.portlet.init-param.view-template<String> = /view.jsp
 javax.portlet.name<String> = candidat_CandidatPortlet
 javax.portlet.resource-bundle<String> = content.Language
 javax.portlet.security-role-ref<String> = power-user,user
References:   (total 1)
 - candidatLocalService: ntlink.db.service.CandidatLocalService UNSATISFIED 1..1 dynamic
   target=(*) scope=bundle

so when i delete the annotation @Reference or when i replace it with @beanReference i got this error

java.lang.NullPointerException: null
    at candidat.portlet.CandidatPortlet.doView(CandidatPortlet.java:59) ~[?:?]
    at com.liferay.portal.kernel.portlet.LiferayPortlet.doDispatch(LiferayPortlet.java:303) ~[portal-kernel.jar:?]
    at com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet.doDispatch(MVCPortlet.java:509) ~[portal-kernel.jar:?]
    at javax.portlet.GenericPortlet.render(GenericPortlet.java:291) ~[portlet.jar:3.0.1]
    at com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet.render(MVCPortlet.java:309) ~[portal-kernel.jar:?]
    at com.liferay.portlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:127) ~[portal-impl.jar:?]
    at com.liferay.portlet.ScriptDataPortletFilter.doFilter(ScriptDataPortletFilter.java:58) ~[portal-impl.jar:?]
    at com.liferay.portlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:124) ~[portal-impl.jar:?]
    at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:71) ~[portal-kernel.jar:?]
    at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:115) ~[portal-kernel.jar:?]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[servlet-api.jar:4.0.FR]
    at org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:153) ~[?:?]
    at org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:62) ~[?:?]
    at org.eclipse.equinox.http.servlet.internal.context.DispatchTargets.doDispatch(DispatchTargets.java:120) ~[?:?]
    at org.eclipse.equinox.http.servlet.internal.servlet.RequestDispatcherAdaptor.include(RequestDispatcherAdaptor.java:48) ~[?:?]
    at com.liferay.portlet.internal.InvokerPortletImpl.invoke(InvokerPortletImpl.java:570) ~[portal-impl.jar:?]
    at com.liferay.portlet.internal.InvokerPortletImpl.invokeRender(InvokerPortletImpl.java:660) ~[portal-impl.jar:?]
    at com.liferay.portlet.internal.InvokerPortletImpl.render(InvokerPortletImpl.java:345) ~[portal-impl.jar:?]
    at com.liferay.portal.monitoring.internal.portlet.MonitoringInvokerPortlet.lambda$render$0(MonitoringInvokerPortlet.java:259) ~[?:?]
    at com.liferay.portal.monitoring.internal.portlet.MonitoringInvokerPortlet._render(MonitoringInvokerPortlet.java:363) ~[?:?] 

so I don't figure out where's my problem and how can I assure that the new database is connected to my project. Ps: my Liferay version is 7.4.3

1

There are 1 answers

0
Olaf Kock On

You will need to deploy your two service-builder jars (the API and the service implementation) to Liferay, so that they are available for the portlet at runtime. At compiletime, Maven finds them (it only needs the API) in your .m2 folder, but at runtime, Liferay doesn't know to check that location - and it doesn't.

As you say you're a beginner in Liferay: I'd start with services through Liferay's standard dependency injection, e.g. OSGi (or "ds", as declarative services are called in the service.xml declaration), instead of adding spring to the game. Adding spring might complicate your situation. I'd strongly recommend to understand service builder first, before adding more complexity.