为什么我收到空指针异常?

时间:2016-10-10 13:37:12

标签: java jsp tomcat cassandra

所以我正在尝试实现一个用户配置文件页面,我已经使用bean,servlet和相应的jsp页面实现了它。

当我尝试访问/ profile / username(该帐户肯定存在于数据库中)时,我的浏览器中显示以下错误:

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /profile.jsp at line 87

84:         <div class="container">
85:             <div class="row">
86:                 <div class="col-lg-12 text-center user-profile">
87:                     <h1>Welcome <%=profile.getUsername()%>!</h1>
88:                 </div>
89:                 <div class="col-sm-4 text-center">
90:                     <div class="user-post" style="background-color: red">


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    [PACKAGE].servlets.Profile.doGet(Profile.java:47)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

java.lang.NullPointerException
    org.apache.jsp.profile_jsp._jspService(profile_jsp.java:218)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    [PACKAGE].servlets.Profile.doGet(Profile.java:47)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

当我去错误所在的位置(servlet Profile.java的第47行)时,它只是:

rd.forward(request, response);

我正在使用与其他servlet完全相同的代码,但它只是不起作用。

我错过了一些非常明显的东西吗?提前致谢。

1 个答案:

答案 0 :(得分:1)

您评论了(删除的答案):

  

抱歉,我应该已经添加了完整的jsp页面,我在上面声明了如下配置文件:

 ProfileStore profile = (ProfileStore) session.getAttribute("Profile");

....但你在servlet代码中设置属性如下:

request.setAttribute("Profile", profile);

你应该使用相同的&#34;范围&#34;设置和获取属性时。如果您获取的属性未在您尝试从中获取的范围中定义,那么您将获得null