shu-chun wrote:
>
> hi, all, i have a question about getparameter
> for example, i have the codes as below:
>
> 1.jsp
> String selectdata="any";
> if (request.getParameter("selectdata").equals("submit")) {
> jobs.selectsingledata();
> }
>
> i have two jsp pages, for example, A.jsp and B.jsp. both A and B go to
> 1.jsp, so i set one paramter in A.jsp to get the difference. if came from
> A.jsp, the jobs.selectsingledata() get executed, but if came from B.jsp, it
> showed the error message like "java.lang.NullPointerException".
> i think the problem is i have to initialize the variable "selectdata", but i
> dont' know how to do it exactly, it seemed that my code above doesn't work
> at all...
That's because you don't send the selectdata parameter to the JSP page
when you request it from B.jsp. request.getParameter("selectdata") will
therefore return null, so you end up with null.equals("submit") for the
last part of your statement. Two ways to fix it:
1) Send the selectdata parameter from B.jsp as well, with a different
value than in A.jsp.
2) Change your code to check for null, e.g.:
String selectdata = request.getParameter("selectdata");
if (selectdata != null && selectdata.equals("submit")) {
jobs.selectsingledata();
}
or
if ("submit".equals(request.getParameter("selectdata"))) {
jobs.selectsingledata();
}
The last alternative takes advantage of the fact that a literal string
is also an object, and that the equals() method accepts null as a
valid argument.
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets