1

I am trying to implement the one example for Criteria API using CriteriaQuery with spring boot and spring data JPA. And when I am running the project , I am getting error Null pointer exception,

Error,

java.lang.NullPointerException: null
at com.spacestudy.controller.AuthenticationController.load(AuthenticationController.java:57) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]

And Test code from my controller is ,

@GetMapping("/load")
    public void load() {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Permissions> cq = cb.createQuery(Permissions.class);
        Root<Permissions> rPermission = cq.from(Permissions.class);
        cq.select(rPermission);
        cq.where(cb.equal(rPermission.get("nuserId"),730));    
        List<Permissions> results = em.createQuery(cq).getResultList();
        for (Permissions permission : results) {
            System.out.println("Number = " + permission.getSpermissionType());
        }
    }

Stacktrace,

Java.lang.NullPointerException: null
at com.spacestudy.controller.AuthenticationController.load(AuthenticationController.java:57) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]

I am new to this Criteria API . Can anyone help me to resolve this issue please?

ooozguuur
  • 3,076
  • 2
  • 20
  • 39
Mr.DevEng
  • 133
  • 1
  • 19

1 Answers1

2

EntityManager should be @PersistenceContext.

@PersistenceContext – We need to understand how we are able to connect with the database using just simple annotation @PersistenceContext and what it is.

 @PersistenceContext
 private EntityManager emf;

 @GetMapping("/load")
 public void load() {
    CriteriaBuilder cb = emf.getCriteriaBuilder();
    CriteriaQuery<Permissions> cq = cb.createQuery(Permissions.class);
    Root<Permissions> rPermission = cq.from(Permissions.class);
    cq.select(rPermission);
    cq.where(cb.equal(rPermission.get("nuserId"),730));    
    List<Permissions> results = em.createQuery(cq).getResultList();
    for (Permissions permission : results) {
        System.out.println("Number = " + permission.getSpermissionType());
    }
}
ooozguuur
  • 3,076
  • 2
  • 20
  • 39
  • Yes. I added but I am getting like - defined in file [C:\spacestudy_workstation\SpaceStudySecurityService\target\classes\com\spacestudy\controller\AuthenticationController.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Persistence annotations are not supported on static fields – Mr.DevEng Aug 29 '18 at 07:46
  • @PersistenceContext private static EntityManagerFactory emf; - this is my code at top in my controller – Mr.DevEng Aug 29 '18 at 07:47
  • Yes,Now I understood the issue was. Its now working. I am able to take sample output. Thank you for your responses and guidance.Its helped me lot. – Mr.DevEng Aug 29 '18 at 08:00
  • @ozgur your code block has low readability and it makes it hard for reusing it in the future. can you make it more readable so it would have a better impact on other developers? here is something you can do: add some blank lines and separate different parts of your code and use better names for your variables so it is more readable for future usage. thank you :) – Mohammad Hossein Shojaeinia Mar 12 '19 at 05:18