Reputation: 2095
I'm trying to make a little application with JSP. It's a homework. So, i have the index.jsp page, where the client enters his name, and if the client exists in the database, then it's making a forward to clientexists.jsp, with the attribute client saved in the session. Then, the client has to make a choice from 2 radio buttons, and depending it's choice, i need to show him his taxes to pay. But after his choice, I have this exception in the Console:
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)
And in the web page, i have another one pointing to the same line:
org.apache.jasper.JasperException: An exception occurred processing JSP page /clientexists.jsp at line 21
Here is my index.jsp page:
<%@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>
And the clientexists.jsp page:
<%@ 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>
Could you help me please? Thanks
Upvotes: 0
Views: 1565
Reputation: 240898
It is here
request.getAttribute("nume").toString();
request.getAttribute("nume")
seems null
and you are invoking toString()
over null
make it like
client = "SOME_DEFAULT_VALUE";
if(request.getAttribute("nume") != null){
client = request.getAttribute("nume").toString();
}
For Client.jsp the error is there because I&Compiler can't see the declaration of client
variable
A suggestion
Upvotes: 3
Reputation: 2879
Typo:
client = request.getAttribute("nume").toString();
Should be:
client = request.getAttribute("name").toString();
getAttribute() returns a null object because it can't find what you are asking it to find and you cant run toString() on null.
One other thing, your html does not need to be in all caps. It's still valid but it's ugly and makes your code look outdated.
Edit: As mentioned by the poster that got in just before myself, you would be wise to separate your application logic from your markup output, it makes things much easier to maintain. Your current design is fine for homework, but it would be less than ideal on a real project.
Upvotes: 0