无法弄清楚这个异常来自哪里?

时间:2011-01-05 10:12:52

标签: java jsp java-ee

我正在尝试用JSP创建一个小应用程序。这是一个功课。所以,我有index.jsp页面,客户端输入他的名字,如果客户端存在于数据库中,那么它将转发到clientexists.jsp,并在会话中保存属性客户端。然后,客户必须从2个单选按钮中进行选择,根据它的选择,我需要向他显示他的税款。但在他选择之后,我在控制台中有这个例外:

18: <INPUT TYPE="SUBMIT" NAME="buton2" VALUE="Continua"></form>
19: 
20: <%
21:     client = request.getAttribute("nume").toString();
22:     
23:     if (request.getParameter("buton2") != null)
24:     {


Stacktrace:] with root cause
java.lang.NullPointerException
    at org.apache.jsp.clientexists_jsp._jspService(clientexists_jsp.java:82)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:363)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:306)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:203)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:242)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

在网页中,我有另一个指向同一行:

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

这是我的index.jsp页面:

<%@page import="dao.*"%>
<%@page import="model.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.*,java.text.*,java.io.*"%>
<jsp:useBean id="rep" class="dao.RepositoryDb"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tax machine</title>
</head>
<body>

<div align="center">

<h1>Your name:</h1>
<form method="POST" action="index.jsp"><INPUT TYPE="TEXT"
    NAME="name"><BR>
<BR>
<INPUT TYPE="SUBMIT" NAME="buton" VALUE="Continua"></form>

</div>
<%
    if (request.getParameter("buton") != null)
    {
        String client = request.getParameter("name");

        if (rep.clientExists(client))
        {
            session.setAttribute("nume", client);
%>
            <jsp:forward page="clientexists.jsp"></jsp:forward>
<%
        }
        else
        {
%>
            <jsp:forward page="clientnotexists.jsp"></jsp:forward>
<%
        }
    }
%>

</body>
</html>

和clientexists.jsp页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%! String client = null; %>
<jsp:useBean id="rep" class="dao.RepositoryDb"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Choose your options</title>
</head>
<body>
<div align="center">

<form method="POST" action="clientexists.jsp">
<INPUT TYPE="RADIO" NAME="group" VALUE=paid" CHECKED>Paid taxes<BR>
<INPUT TYPE="RADIO" NAME="group" VALUE="unpaid">Unpaid taxes<BR>
<BR>
<INPUT TYPE="SUBMIT" NAME="buton2" VALUE="Continue"></form>

<%
    client = session.getAttribute("nume").toString();

    if (request.getParameter("buton2") != null)
    {
        String option = request.getParameter("group");

        if (option.equalsIgnoreCase("paid"))
        {
%>
            <%= rep.getImpozitePlatite(client).toString()%>
<%
        }
        else
        {
%>
            <%= rep.getImpoziteNeplatite(client).toString()%>
<%
        }
    }
%>
</div>
</body>
</html>
你能帮帮我吗?感谢

2 个答案:

答案 0 :(得分:3)

就在这里

request.getAttribute("nume").toString();

request.getAttribute("nume")似乎null并且你在toString()上调用了null

使其像

client = "SOME_DEFAULT_VALUE";
if(request.getAttribute("nume") != null){
client = request.getAttribute("nume").toString();
}

对于Client.jsp,错误是因为我和编译器无法看到client变量的声明

建议

答案 1 :(得分:0)

错字:

client = request.getAttribute("nume").toString();

应该是:

client = request.getAttribute("name").toString();

getAttribute()返回一个null对象,因为它无法找到你要求它查找的内容而你无法在null上运行toString()。

另外一件事,你的html不需要全部大写。它仍然有效,但它很难看,使你的代码看起来过时了。

编辑如前面提到的海报所提到的那样,将应用程序逻辑与标记输出分开是明智之举,它使事情更容易维护。你目前的设计很适合做作业,但在实际项目中它并不理想。