0

I'm trying to make a restfull endpoint using Jersey to an existing application.

When I try to get a list of data (Mails) from my database I get the following error:

java.lang.NullPointerException
    product.security.restcontroller.MailController.allMails(MailController.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

This is my rest controller:

package product.security.restcontroller;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import product.security.model.Mail;
import product.security.service.entityservice.MailService;

@Path("/mails")
public class MailController {

private MailService mailService;
private List<Mail> allMails;


@GET
@Path("/all")
public List<Mail> allMails() throws Exception {
    allMails = mailService.findAll();
    return allMails;
    }
}

And this is my service function

@Override
    public List<Mail> findAll() throws Exception {
        Query query = em.createQuery("SELECT e FROM Mail AS e");
        return (List<Mail>) query.getResultList();
    }

Mail service

< bean id="mailService"
    class="product.security.service.entityservice.MailServiceImpl">
</bean>

Please note that the application is already running using struts, and the service above works as expected

Update:

Seems like I have a problem with my services : When I called mailService.findAll() inside my controller,using Jeresey, I dont go to findAll() function, and it throws NullPointerException.

The same way, I tried to call findAll() function directly using Jeresy like below :

@GET
@Path("/all")
 public List<Mail> findAll() throws Exception {
        Query query = em.createQuery("SELECT e FROM Mail AS e");
        return (List<Mail>) query.getResultList();
    }

Which did not work eather, and I got a NULL pointer Exception at the line which contains em instance. Knowing that Entity Manager is also managed by using services..

I have already separated xml file which manage the services beans, but it seems like Jeresy cannot see it somehow

Faouzi
  • 929
  • 2
  • 15
  • 23
  • Possibly NULL entry in database can't be deserialized? I assume You don't want such entries anyway so you can update query +" + WHERE e IS NOT NULL" (or what is appropripate for your db) – rAndom69 May 14 '19 at 16:47
  • When is mailService ever initialized? – Paul Samsotha May 15 '19 at 03:50
  • @rAndom69, It didn't work either.. I'm thinking maybe there is some logic that verify if some user is currently logged in before getting the data ? but I'm not sure.. as I said the application is already developed using struts2 (not by me) – Faouzi May 15 '19 at 11:28
  • @PaulSamsotha I dont see how this is a duplication to the question you marked above.. mailService is initialized in ServiceLayerContext Xml file < bean id="mailService" class="... – Faouzi May 15 '19 at 15:35
  • If `mailService.findAll();` is line 40, then it isn't being initialized. – Paul Samsotha May 15 '19 at 16:27
  • Yes, turn out this is the problem, but it has to be initialized in the serviceLayerContext.xml file, which is not the case. Any idea why ? – Faouzi May 16 '19 at 10:59

0 Answers0