Foren

Why am I getting the value returned by getService() as null?

thumbnail
Abhishek Jain, geändert vor 7 Jahren.

Why am I getting the value returned by getService() as null?

Regular Member Beiträge: 226 Beitrittsdatum: 20.08.16 Neueste Beiträge
I am making a custom-sql appication. I made a service builder , created finder column,built and deployed it. Table created successfully. Now I made a *FinderImpl class manually and a custom-sql folder with necessary details and rebuilt the services. *Finder interface generated successfully. I implemented *Finder interface in *FinderImpl class, then rebuilt services but *FinderUtil not generated. I ignored it as I could easily call *FinderImpl class' method directly. I created a custom method inside *FinderImpl class and rebuilt the service. I am calling this method inside *LocalServiceImpl and calling the method of *LocalServiceImpl through *LocalServiceUtil. But to my surprise I got the most common error-"NullPointerException".I debugged and found that getService() method of *LocalServiceUtil has value as null. Please tell me how to eradicate this error as I have been stuck throughout the day but could not resolve. So,please help. Thanx in advance.
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Why am I getting the value returned by getService() as null?

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
So this is a common complaint currently, but your solution is all wrong.

In your *LocalServiceImpl, you'll find that you have injected an instance of *Finder. You should define your method to expose the query in *LocalServiceImpl and let it call the *Finder method for you.

Actually this is good, clean separation of the data layer. Having external code call *Finder directly kind of violates the separation of the data access layer API by exposing implementation details.

If you're doing pure OSGi portlet development, you should not be using any of the Util classes, btw. In pure OSGi, you should let OSGi inject the service reference into your code.
thumbnail
Abhishek Jain, geändert vor 7 Jahren.

RE: Why am I getting the value returned by getService() as null?

Regular Member Beiträge: 226 Beitrittsdatum: 20.08.16 Neueste Beiträge
I have defined a method inside *LocalServiceImpl as follows:-
public List<CustomUser2> findByname(String name)
{
return customUser2Finder.findByname(name,0,100);
}

Inside this method I am using *Finder instance as you said to call *Finder method. But how to call this method (findByname(String)) without using *LocalServiceUtil.
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Why am I getting the value returned by getService() as null?

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
You shouldn't, from a separation of concerns perspective.

Since you have the method exposed in *LocalServiceImpl, that's all that matters.
thumbnail
Abhishek Jain, geändert vor 7 Jahren.

RE: Why am I getting the value returned by getService() as null?

Regular Member Beiträge: 226 Beitrittsdatum: 20.08.16 Neueste Beiträge
But how to call *LocalServiceImpl's method without using *LocalServiceUtil ?
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Why am I getting the value returned by getService() as null?

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
The pure OSGi way is to use the @Reference annotation and let OSGi inject it. This also works with the custom finder instance.

If you're not in pure OSGi mode, then it's best to go through the Util class as it removes OSGi dependencies from your legacy code.
Revathi Vadlamudi, geändert vor 7 Jahren.

RE: Why am I getting the value returned by getService() as null?

Junior Member Beiträge: 82 Beitrittsdatum: 14.11.14 Neueste Beiträge
Me too getting the same issue. Did you resolve the issue or where I have to give @Reference. @David can you please tell me.
thumbnail
krishna mohan mathakala, geändert vor 6 Jahren.

RE: Why am I getting the value returned by getService() as null?

Junior Member Beiträge: 68 Beitrittsdatum: 08.09.12 Neueste Beiträge
Hi I am also getting the same issue, Did any one found solution for this.?
lando scotto, geändert vor 5 Jahren.

RE: Why am I getting the value returned by getService() as null?

New Member Beiträge: 3 Beitrittsdatum: 18.11.09 Neueste Beiträge
Hy,
​​​​​​​I had the same problem I solved by adding a finder to the service.xml and implementing the class in this way "public class GuestbookFinderImpl extends BasePersistenceImpl <Guestbook> implements GuestbookFinder"