Fórum

Null pointer Exception in Dynamic Query

Amresh kumar, modificado 6 Anos atrás.

Null pointer Exception in Dynamic Query

New Member Postagens: 20 Data de Entrada: 17/05/12 Postagens Recentes
How would you replicate this SQL with an equivalent Liferay DynamicQuery :


select name from role_ where roleId in (
SELECT distinct roleId from resourcepermission where name in (
SELECT distinct name FROM resourceaction where actionId = 'SAMPLE'));


I have tried with this but getting below exception :

List<String> permissionNames=new ArrayList<String>();
List<String> portalRoles = null;
permissionNames.add("SAMPLE");
permissionNames.add("SAMPLE1");

ClassLoader cl = PortalClassLoaderUtil.getClassLoader();
DynamicQuery roleQuery = DynamicQueryFactoryUtil.forClass(Role.class,cl);
roleQuery.add(PropertyFactoryUtil.forName("roleId").in(DynamicQueryFactoryUtil.forClass(ResourcePermission.class,cl).add(PropertyFactoryUtil.forName("name").in( DynamicQueryFactoryUtil.forClass(ResourceAction.class,cl).add(PropertyFactoryUtil.forName("actionId").in(permissionNames)))))).setProjection(ProjectionFactoryUtil.property("name"));
portalRoles = RoleLocalServiceUtil.dynamicQuery(roleQuery,-1,-1); // here i am getting the null pointer exception

10:21:18,964 ERROR [http-bio-8080-exec-359][BasePersistenceImpl:244] Caught unexpected exception java.lang.NullPointerException

Can anyone help me to resolve this issue.

Thanks in advance !!!!

Thanks,
Amresh
thumbnail
Andrew Jardine, modificado 6 Anos atrás.

RE: Null pointer Exception in Dynamic Query

Liferay Legend Postagens: 2416 Data de Entrada: 22/12/10 Postagens Recentes
Can you provide us with the full stacktrace? The devil is in the details as the trace will likely tell us which class in the core (and which line) is throwing the exception. Just as a note, personally, I don't normally do nested dynamic queries like that for a couple of reasons.

1. Readability -- it's a nightmare the way you have it right now.

2. If you split them out then you are able to actually validate that one query worked before attempting the second. That way if you do get an error it's easier to diagnose what part of the query is causing the issue.

For now though, stacktrace if you can. Please be sure to include it in the <> (code tags) so that it is readable.
thumbnail
Jorge Díaz, modificado 6 Anos atrás.

RE: Null pointer Exception in Dynamic Query

Liferay Master Postagens: 753 Data de Entrada: 09/01/14 Postagens Recentes
Try always replacing DynamicQueryFactoryUtil.forClass({className}.class,cl) with {className}LocalServiceUtil.dynamicQuery().
That will avoid classloader issues

More info see https://web.liferay.com/community/forums/-/message_boards/message/90754477

Also follow Andrew Jardine comment advices.