Servlet.service() for servlet jsp threw exception java.lang.NullPointerException while calling EJB bean

While calling EJB bean on my simple jsp page I have such an error:

 WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw 
 exception java.lang.NullPointerException
at appjpa.ejb.UsersFacade.findAllStudents(UsersFacade.java:65)
at org.apache.jsp.Students_jsp._jspService(Students_jsp.java from :75)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

Code for my jsp page is such:

<%@page import="appjpa.entities.Users"%>
<%@page import="java.util.ArrayList"%>
<%@page import="socnet2.Student"%> <!-- class Student emulates real student -->
<%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <jsp:useBean class="appjpa.ejb.UsersFacade" id="dao" scope="application"></jsp:useBean>
    <p><b>List of all students:</b></p>
    <form action="/AppJPA/Edit" method="POST">
       <jsp:scriptlet>
       int counter = 0;  if (dao != null){
       for (Users s : dao.findAllStudents()) {
        </jsp:scriptlet>
        <p> <jsp:expression> s.getName() + " " + s.getSurname()</jsp:expression></p>
        <input type="submit" name="Edit_<%=counter%>" value="Edit" />
        <input type="submit" name="Delete_<%=counter%>" value ="Delete"/> 
         <jsp:scriptlet>
                counter++;
            }}
        </jsp:scriptlet>
        <input type="submit" name="Add_new_student" value ="Add new student"/>
    </form>
</body>

Autogenerated by NetBeans 7.0.1 wizard entity class for users looks like:

 @Entity
@Table(name = "USERS")
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"),
@NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name =      :name"),
@NamedQuery(name = "Users.findBySurname", query = "SELECT u FROM Users u WHERE u.surname = :surname"),
@NamedQuery(name = "Users.findByLogin", query = "SELECT u FROM Users u WHERE u.login = :login"),
@NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
@NamedQuery(name = "Users.findByRole", query = "SELECT u FROM Users u WHERE u.role = :role")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
// @NotNull
@Column(name = "ID")
private Integer id;
@Size(max = 40)
@Column(name = "NAME")
private String name;
@Size(max = 40)
@Column(name = "SURNAME")
private String surname;
@Size(max = 40)
@Column(name = "LOGIN")
private String login;
@Size(max = 40)
@Column(name = "PASSWORD")
private String password;
@Size(max = 40)
@Column(name = "ROLE")
private String role;
@ManyToMany(mappedBy = "usersList")
private List<SubjectGroup> subjectGroupList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idCreator")
private List<SubjectGroup> subjectGroupList1;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idSender")
private List<Messages> messagesList;

public Users() {
}

public Users(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getRole() {
    return role;
}

public void setRole(String role) {
    this.role = role;
}

public List<SubjectGroup> getSubjectGroupList() {
    return subjectGroupList;
}

public void setSubjectGroupList(List<SubjectGroup> subjectGroupList) {
    this.subjectGroupList = subjectGroupList;
}

public List<SubjectGroup> getSubjectGroupList1() {
    return subjectGroupList1;
}

public void setSubjectGroupList1(List<SubjectGroup> subjectGroupList1) {
    this.subjectGroupList1 = subjectGroupList1;
}

public List<Messages> getMessagesList() {
    return messagesList;
}

public void setMessagesList(List<Messages> messagesList) {
    this.messagesList = messagesList;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Users)) {
        return false;
    }
    Users other = (Users) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "appjpa.entities.Users[ id=" + id + " ]";
}

}

And, finaly, class UsersFacade is such:

@Stateless
public class UsersFacade extends AbstractFacade<Users> implements UsersFacadeLocal {
@PersistenceContext(unitName = "AppJPAPU")
private EntityManager em;

protected EntityManager getEntityManager() {
    return em;
}

public UsersFacade() {
    super(Users.class);
}

@Override
public Users checkInBase(String login, String password) {
    Users u = (Users)(em.createNamedQuery("Users.findByLogin").setParameter("login",      login).getSingleResult());

    return u;
}

@Override
public void removeUserFromSubjectGroup(Users user, SubjectGroup group) {
    Users u = em.find(Users.class, user.getId());
    SubjectGroup g = em.find(SubjectGroup.class, group.getId());
    if (u != null && g != null) {
        user.getSubjectGroupList().remove((SubjectGroup)group);
        group.getUsersList().remove(user);
        em.merge(user);
        em.merge(group);
    }
}

@Override
public void addUserToSubjectGroup(Users user, SubjectGroup group) {
    Users u = em.find(Users.class, user.getId());
    SubjectGroup g = em.find(SubjectGroup.class, group.getId());
    if (u != null && g != null) {
        user.getSubjectGroupList().add((SubjectGroup)group);
        group.getUsersList().add(user);
        em.merge(user);
        em.merge(group);
    }
}

@Override
public List<Users> findAllStudents() {
    String role = new String("student");
     List<Users> students = (List<Users>)(em.createNamedQuery("SELECT u FROM Users u WHERE u.role = :role").setParameter("role", role).getResultList());
     if (students == null){
         System.out.println("Students are null!!!");
     }

    return students;
}

@Override
public boolean deleteStudent(Users user) {
    Users u = em.find(Users.class, user.getId());
    em.remove(user);
    return true;
}

@Override
public boolean updateStudent(Users studentToEdit, String newName, String newSurname, String newLogin, String newPassword) {
    Users u = em.find(Users.class, studentToEdit.getId());
    u.setName(newName);
    u.setSurname(newSurname);
    u.setLogin(newLogin);
    u.setPassword(newPassword);
     em.merge(studentToEdit);
    return true;
}

@Override
public void insertStudenttoDB(Users student, String newName, String newSurname, String newLogin, String newPassword) {
   em.persist(student);
   Users u = em.find(Users.class, student.getId());
    if (u != null) {
    student.setName(newName);
    student.setSurname(newSurname);
    student.setLogin(newLogin);
    student.setPassword(newPassword);  
    em.merge(student);
    }
}     
}

I am quite new to EJB and maybe I am not using them right. I tried calling other methods from UsersFaced at another pages, but received the same error. Please, explain what I am doing wrong…

StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception

2012-03-23 10:42:08 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException at org.apache.jsp.jsp.TitleFrame_jsp._jspService(TitleFrame_jsp.java:66)

javax.servlet.ServletException: Servlet.destroy() for servlet jsp threw exception

From time to time i get this error on Eclipse: Grave: Servlet jsp threw unload() exception javax.servlet.ServletException: Servlet.destroy() for servlet jsp threw exception at org.apache.catalina.core

Servlet.service() for servlet jsp threw exception java.lang.NullPointerException

i am getting below exception and tomcat hangs and my services are down. SEVERE: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException at org.apache.jsp.search_005fresult_jsp

Accessing Stateful session bean from servlet/jsp throws ClassCast exception in jboss 4.2.2

Whenever I try to access EJB 3 Stateful session bean deployed on JBoss 4.2.2 application server using a web client(within JBoss server) such as servlet/jsp , I get following exception java.lang.ClassC

WARNING: StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException glassfish

hi I am new to java web programming. Have written two simple jsp pages. one to accept the name, and second to show the entered name. I dont know what and where is wrong with my code. Can any one help

WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException

I keep getting this warning, on a VERY simple JSP/Bean Page, I am practicing for exams, and I can’t get past this. Could someone point out my foolishness please? <html> <head> <title&gt

JSP calling method Servlet

I would like to have an input ‘submit’ calling a method from the Java EE java servlet that is responsible for showing the JSP. <form action=???? method=post> <input type=image src=img/

javax.servlet.ServletException: Servlet.init() for servlet Relay threw exception

I built application by using Netbeans and its working fine. But When i deployed on TOMCAT I am getting this error javax.servlet.ServletException: Servlet.init() for servlet Relay threw exception org.

java ejb jsp unreported exception

trying to use a jsp to interact with a bean however, get some errors when running it. Here are the errors I see in the browser Generated servlet error: [javac] C:/Sun/AppServerNew/domains/domain1/gene

‘GinjectorGenerator’ threw an exception while rebinding ‘…ClientGinjector’

I’m getting this error when running my web application that use GWT, GWT-Platform and App engine: Generator ‘com.google.gwt.inject.rebind.GinjectorGenerator’ threw an exception while rebinding ‘com.m

Answers

If this is your whole pack of named queries than I guess that this code:

 List<Users> students = (List<Users>)(em.createNamedQuery(
         "SELECT u FROM Users u WHERE u.role = :role").setParameter("role", role)
         .getResultList());

is messed up. You’re trying to create named query which name is equal to SELECT u FROM Users u WHERE u.role = :role.

You have named queries defined in your Users annotation and the one you’re trying to use is this one:

@NamedQuery(name = "Users.findByRole", 
            query = "SELECT u FROM Users u WHERE u.role = :role")})

Therefore you should use it’s name – Users.findByRole. Moreover, you don’t need to cast to List<Users> as there is an appropriate method parameter to define result type. It should be rather:

List<Users> students = em.createNamedQuery("Users.findByRole", Users.class)
                         .setParameter("role", role).getResultList();

There are few bad practices in your code like:

  • using plural for your entities; it should be rather User than Users,
  • using scriptlets; and using <jsp:scriptlet> doesn’t make it any better.

The @PersistenceContext won’t be injected if you manually instantiate an EJB class as a simple java bean in JSP by <jsp:useBean> instead of getting a container managed instance by @EJB. I don’t know how and where you learned to write JSPs that way, but it’s undoubtely based on a heavily outdated book/tutorial. You need to make drastic changes in your current model-view-without-controller approach. You need to add a controller. Throw all those <jsp:xxx> tags away and create a Servlet which acts as a real controller.

E.g.

@WebServlet("/students")
public class StudentsServlet extends HttpServlet {

    @EJB
    private UsersFacade usersFacade;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("users", usersFacade.findAllStudents());
        request.getRequestDispatcher("/WEB-INF/students.jsp").forward(request, response);
    }

}

Note that I moved your JSP into /WEB-INF folder so that it’s not possible to either purposefully or accidently invoke it without calling the servlet.

Now, change your JSP as follows:

<%@page pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h4>List of all students:</h4>
        <form action="${pageContext.request.contextPath}/edit" method="post">
            <c:forEach items="${users}" var="user" varStatus="loop">
                <p><c:out value="${user.name} ${user.surname}" /></p>
                <input type="submit" name="Edit_${loop.index}" value="Edit" />
                <input type="submit" name="Delete_${loop.index}" value ="Delete"/> 
            </c:forEach>
            <input type="submit" name="Add_new_student" value ="Add new student"/>
        </form>
    </body>
</html>

Open it by the servlet’s URL http://localhost:8080/AppJPA/students so that its doGet() will to the right job and that the JSTL <c:forEach> will iterate over users accordingly.

I have by the way my doubts about the way how you named the buttons. Identifying the selected user would be very clumsy. But that’s subject for a different question.

See also: