0

I am very new to JavaEE. I've lookup a lot of questions about NullPinterException with EJB injection in this site and others, but have no success yet. I can't find the solution for my problem.

I created DynamicWebProject in Eclipse with JPA, JAX-RS, Glassfish Web Extensions facets. Here is the sctructure of my project (web.xml file is under the glassfish-web.xml file):

Project Structure


I try to call getemployees webservice which deployed to localhost Glassfish server and try to get information about all employees from my database (also at localhost). Here is the code of my GetEmployees class.

package bean;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.google.gson.Gson;

import ejb.EmployeeEJB;

@ApplicationScoped
@ManagedBean
@Path("/getemployees")
public class GetEmployees {

    @Inject
    EmployeeEJB employeeEJB;

    @GET
    @Produces("application/json; charset=utf-8")
    public Response getAllEmployees() throws Exception {
        Gson gson = new Gson();
        String jsonString = null;
        try {
            jsonString = gson.toJson(employeeEJB.getAll());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.status(200).entity(jsonString).build();
    }
}

I created Managed and Session beans. I made Managed bean from my GetEmployees RESTful webservice class. Is it correct?

EmployeeEJB is the session bean:

package ejb;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entity.Employee;

@LocalBean
@Stateless
public class EmployeeEJB {

    @PersistenceContext
    EntityManager em;

    public void saveEmployee(Employee employee) {
        em.merge(employee);
    }

    public void deleteEmployee(Employee employee) {
        em.remove(employee);
    }

    public List<Employee> getAll() {
        Query query = em.createQuery("SELECT emp FROM Employee emp", Employee.class);
        List<Employee> emp = (List<Employee>) query.getResultList();
        return emp;
    }

}

Here is my persistence.xml file. JDBC connection pool and JDBC resource created in Glassfish admin console. Connection to database is Ok.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="FirstDynamicWebProject">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/second_attempt_hibernate</jta-data-source>
    <class>entity.Docstatus</class>
    <class>entity.Doctype</class>
    <class>entity.Document</class>
    <class>entity.Employee</class>
    <class>entity.Mailorder</class>
    <class>entity.Mailorderstatus</class>
    </persistence-unit>
</persistence>

NullPointerException occur when I try to call an employeeEJB.getAll() method in GetEmployees class:

2017-04-10T00:59:07.063+0500|Severe: java.lang.NullPointerException
    at bean.GetEmployees.getAllEmployees(GetEmployees.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)

I think the injection of EmployeeEJB is not correct, but have no ideas how to win it.

Rinat
  • 362
  • 3
  • 14

1 Answers1

1

I guess the problem is that you are using the wrong package for @ApplicationScoped. And you don't need the @ManagedBean there I think.

You are using:

import javax.faces.bean.ApplicationScoped;

Try the following:

import javax.enterprise.context.ApplicationScoped;

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
  • Thank you for help, man! I did't think it would be so easy! Thanks again! – Rinat Apr 09 '17 at 20:48
  • I've just tried to deploy war-file of the project to another Glassfish server, which is installed on remote host and got the same exception in server.log. What can I do? I've try to clean project, build again, clean again build and deploy, but nothing help. Have you any ideas? – Rinat Apr 09 '17 at 21:18
  • Currently I have no real idea except trying yet another Glassfish installation. Are you sure it is really the same exception? – unwichtich Apr 09 '17 at 22:43
  • Maybe problem is in that localhost - Windows and remote host - Linux? – Rinat Apr 10 '17 at 22:10
  • That shouldn't make a difference. – unwichtich Apr 11 '17 at 08:45