1

It looks something is going wrong with persist method of EntityManager class. I use apache tomcat and recieve HTTP Status 500- page every time I try.

This is DataQuery class:

public class DataQuery {
    EntityManagerFactory emf;
    EntityManager em;

    public DataQuery() {
        emf = Persistence.createEntityManagerFactory("com.sbu.ac.jobyab");
        em = emf.createEntityManager();
        em.getTransaction().begin();
    }

    public void registerUser(Employee e){
        em.persist(e);
    }
}

And this is the registerController class that recieves input from user and do the registeration task:

public class registerController extends HttpServlet {
    private DataQuery query;
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String email = request.getParameter("register_email");
        String pwd = request.getParameter("register_password");
        Employee e = new Employee(10, email, pwd);
        query.registerUser(e);
    }
}

This is full stack error :

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException com.jobyab.controllers.registerController.doPost(registerController.java:39) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) note The full stack trace of the root cause is available in the Apache Tomcat/8.0.33 logs.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
inverted_index
  • 2,329
  • 21
  • 40

3 Answers3

1

try with adding query = new DataQuery(); inside doPost() method

ypp
  • 144
  • 2
  • 8
0

looks like this is null

private DataQuery query;

It should probably be injected using @Inject or @EJB

@EJB
private DataQuery query;

But could be something else, whats on line 39 of your controller ?

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

I don't believe you Tomcat comes with an EJB container or CDI Container, so the possibility of injecting it with an @EJB or @Inject notation is off. You might have to programmatically create a DataQuery instance and use it, as explained by @ypp.

João Rebelo
  • 79
  • 14