I just ran my first JSP that uses a form and got an
error. Unfortunately, the stack trace doesn't tell me
where in the JSP the error occured (that I can tell)
or where in my helper bean the problem ight be. I'd
heard that JSPs are hard to debug, and now I see why.
The JSP engine is not very helpful with what's wrong.
Here's the stack trace, followed by the JSP code.
Location: /examples/jsp/form/Form.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Attempted a bean
operation on a null object.
at
org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty(JspRuntimeLibrary.java:424)
at
jsp.f_00025rm._0002fjsp_0002fform_0002fForm_0002ejspForm_jsp_0._jspService(_0002fjsp_0002fform_0002fForm_0002ejspForm_jsp_0.java:122)
at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at
org.apache.tomcat.core.Handler.service(Handler.java:286)
at
org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at
org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at
org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at
org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
JSP code:
<html>
<body bgcolor="#c8d8f8">
<form action="/examples/jsp/form/Form.jsp"
method=post>
<center>
<table cellpadding=4 cellspacing=2 border=0>
<th bgcolor="#CCCCFF" colspan=2>
<font size=5>User Registration</font>
</th>
<tr>
<td valign=top>
<b>First Name</b>
<br>
<input type="text" name="firstName" size=15></td>
<td valign=top>
<b>Last Name</b>
<br>
<input type="text" name="lastName" size=15></td>
</tr>
<tr>
<td valign=top colspan=2>
<b>E-Mail</b>
<br>
<input type="text" name="email" size=20>
<br></td>
</tr>
<tr>
<td valign=top colspan=2>
<b>What languages do you program in?</b>
<br>
<input type="checkbox" name="languages"
value="Java">Java
<input type="checkbox" name="languages"
value="C++">C++
<input type="checkbox" name="languages"
value="C">C<br>
<input type="checkbox" name="languages"
value="Perl">Perl
<input type="checkbox" name="languages"
value="COBOL">COBOL
<input type="checkbox" name="languages"
value="VB">VB<br>
</td>
</tr>
<tr>
<td valign=top colspan=2>
<b>How often can we notify you regarding your
interests?</b>
<br>
<input type="radio" name="notify" value="Weekly"
checked>Weekly
<input type="radio" name="notify"
value="Monthly">Monthly
<input type="radio" name="notify"
value="Quarterly">Quarterly
<br></td>
</tr>
<tr>
<td align=center colspan=2>
<input type="submit" value="Submit"> <input
type="reset" value="Reset">
</td>
</tr>
</table>
</center>
</form>
<%-- Create the bean only when the form is posted --%>
<%
if (request.getMethod().equals("POST"))
{
%>
<jsp:useBean id="formHandler"
class="com.shopping.FormBean">
<%-- provide a setProperty tag and ensure that the
setter methods are invoked via introspection
--%>
<jsp:setProperty name="formHandler" property="*"/>
</jsp:useBean>
<p>
<hr>
<font color=red>
<b>You submitted:<P>
First Name:</b><br>
<%-- invoke the getter method to display the firstName
using the getProperty tag --%>
<jsp:getProperty name="formHandler"
property="firstName"/><br>
<br><b>Last Name:</b><br>
<%-- invoke the getter method to display the lastName
using the getProperty tag --%>
<jsp:getProperty name="formhandler"
property="lastName"/><br>
<br><b>Email:</b><br>
<%-- invoke the getter method to display the email
address using the getProperty tag
<jsp:getProperty name="formHandler"
property="email"/><br>
--%>
<b>Languages:</b><br>
<%
String[] lang = formHandler.getLanguages();
if (!lang[0].equals("1"))
{
out.println("<ul>");
for (int i=0; i<lang.length; i++)
out.println("<li>"+lang[i]);
out.println("</ul>");
}
else out.println("Nothing was selected<br>");
%>
<b>Notification:</b><br>
<%-- invoke the getter method to display the
notification status using the getProperty tag
--%>
<jsp:getProperty name="formHandler"
property="notify"/><br>
<br>
<%
}
%>
</font>
</body>
</html>
I think this would be better as an attachment but lots
of mailing lists don't like those, so ugly or not,
there it is. Thanks.
Ken
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/