Java EJB org.apache.jasper.JasperException:java.lang.NullPointerException

时间:2012-11-25 20:42:25

标签: java jsp ejb javabeans netbeans-7

我为在线DVD商店创建了一个EJB应用程序。 我使用Netbeans 7.2.1和Glassfish 3.1.2 当我运行index.jsp时,它成功启动界面,如果我点击表单提交按钮,我收到此错误:

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
root cause

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the  GlassFish Server Open Source Edition 3.1.2.2 logs.

在Netbeans中我收到此错误:

INFO: Couldn't create the bean Lookup failed for 'businessSessionBeans.UserSessionBeanRemote' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:76)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    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:722)

我创建了一个Java EE企业应用程序。我有项目-ejb和项目战。因为我无法直接从ejb创建SessionBean,所以我创建了一个新的项目/ Java库类,并在那里创建了实体。我将库引入了ejb和war,他们识别了实体,因此我能够创建会话bean。 但我得到了这个错误,我不知道这个问题,请参阅一些消息来源:

的index.jsp

    <%@page import="businessSessionBeans.* , java.math.*, javax.naming.*" %>

<%!
    private UserSessionBeanRemote userB=null;
    public void jspInit()
    {
        try{
            InitialContext ic=new InitialContext();
            userB=(UserSessionBeanRemote)ic.lookup(UserSessionBeanRemote.class.getName());
        }catch(Exception e)
                {System.out.println("Couldn't create the bean "+e.getMessage());}

     }
   public void jspDestroy()
   {
       userB=null;
   }
%>

        <%  String username=(String)request.getParameter("username");
            String password=(String)request.getParameter("password");
            if((username!=null)&(password!=null)){
                String userType=userB.userLogin(username,password);
                if (userType!=null){
                    session.setAttribute("username",username);
                    session.setAttribute("password",password);
                    session.setAttribute("userId", userB.getUserId(username, password));
                    if (userType.equals("Admin"))
                       response.sendRedirect("admin.jsp");
                    else if (userType.equals("User"))
                         response.sendRedirect("user.jsp");
                }
                else{
        %> <script type="text/javascript">
                window.alert("User not registered");
        </script>
            <%}}%>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Login</title>
        </head>
        <body>
            <h1>Welcome to our DVD Store!</h1>
        <form method="post" action="index.jsp">
            <pre>
            Username: <input type="text" name="username"/><br/>
            Password: <input type="password" name="password"/><br/>
            <input type="submit" value="Submit"/>  <input type="reset" value="Reset"/><br/>
        <h3>Don't have an account? <a href="register.jsp">Sign up</a></h3>
            </pre>
        </form>
    </body>
</html>

UserSessionBeanRemote.java

public interface UserSessionBeanRemote {

public void create(java.lang.String username, java.lang.String password, java.lang.String name, java.lang.String type);

public void edit(Entities.UserEntityBean userBean);

public void destroy(java.lang.Integer id);

public Entities.UserEntityBean find(java.lang.Object pk);

public java.util.List findAll();

public java.lang.String userLogin(java.lang.String username, java.lang.String password);

public java.lang.Integer getUserId(java.lang.String username, java.lang.String password);

}

UserSessionBean.java

public class UserSessionBean implements UserSessionBeanRemote {

// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
@PersistenceContext
EntityManager em;

@Override
public void create(String username,String password,String name, String type)
{
    UserEntityBean userBean=new UserEntityBean();
    userBean.setName(username);
    userBean.setPassword(password);
    userBean.setName(name);
    userBean.setUserType(type);
    em.persist(userBean);
}

@Override
public void edit(UserEntityBean userBean)
{
    em.merge(userBean);
}

@Override
public void destroy(Integer id)
{
    UserEntityBean userBean=(UserEntityBean)em.find(UserEntityBean.class, id);
    em.remove(userBean);
}

@Override
public UserEntityBean find(Object pk)
{
    return (UserEntityBean)em.find(UserEntityBean.class, pk);
}

@Override
public List findAll()
{
    return em.createQuery("select OBJECT(o) from UserEntityBean o").getResultList();
}

@Override
public String userLogin(String username,String password)
{

    String fusername,fpassword;
    UserEntityBean userBean;
    List userList=findAll();
    Iterator it=userList.iterator();
    while(it.hasNext())
    {
        userBean=(UserEntityBean)it.next();
        fusername=userBean.getName();
        fpassword=userBean.getPassword();
        if(fusername.equals(username)&fpassword.equals(password))
        {
            return userBean.getUserType();
        }   
    }
    return null;
}

@Override
public Integer getUserId(String username,String password)
{

    String fusername,fpassword;
    UserEntityBean userBean;
    List userList=findAll();
    Iterator it=userList.iterator();
    while(it.hasNext())
    {
        userBean=(UserEntityBean)it.next();
        fusername=userBean.getName();
        fpassword=userBean.getPassword();
        if(fusername.equals(username)&fpassword.equals(password))
        {
            return userBean.getId();
        }
    }
    return null;
}

}

最后,UserEntityBean.java

public class UserEntityBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
private String userType;

public Integer getId() {
    return id;
}

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

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return the password
 */
public String getPassword() {
    return password;
}

/**
 * @param password the password to set
 */
public void setPassword(String password) {
    this.password = password;
}

/**
 * @return the userType
 */
public String getUserType() {
    return userType;
}

/**
 * @param userType the userType to set
 */
public void setUserType(String userType) {
    this.userType = userType;
}

@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 UserEntityBean)) {
        return false;
    }
    UserEntityBean other = (UserEntityBean) 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 "Entities.UserEntityBean[ id=" + id + " ]";
}

}

任何想法?

1 个答案:

答案 0 :(得分:0)

您应该像以下一样查找EJB:

userB=(UserSessionBeanRemote)ic.lookup("UserSessionBean");

你的代码缺少@Stateless:

@Stateless(name="UserSessionBean") 
public class UserSessionBean implements UserSessionBeanRemote {