Re: strange problem with request.getQueryString()
maya wrote:
I have a weird problem:
don't understand this error:
org.apache.jasper.JasperException: Exception in JSP index.jsp:6
3: String qs = "";
4: qs = request.getQueryString();
5:
6: if (qs.indexOf("pn") != -1) {
7: out.println("yes");
8: } else if (qs.indexOf("pn") == -1){
9: out.println("no");
java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService (index_jsp.java:88)
now line 88 in the JSP servlet is this:
if (qs.indexOf("pn") != -1) {
how can this be??
'qs' is 'null'.
One question mark suffices to indicate an interrogative.
I did a bare-bones example here,
http://www.mayacove.com/dev/test_qs.jsp
you click on the link and it takes you to another pg, url contains a
query string, same conditional I posted here works fine in this test
example, but in my webapp, for some very odd reason, wherever I put this
code, I get a NPE on the var that evaluates to the query string in the
url..
One period suffices to indicate a declarative.
however, if I test like this:
if (qs != null) or if (qs == null)
I don't get an error, but if I test for what's contained in the query
string I get the NPE on qs var.. is this weird or what??
It's weird to test for 'null', find that the variable does actually equal
'null', then try to dereference it anyway. The point of the test is to bypass
code that would otherwise throw an NPE.
NPE is a consequence of programmer error. If 'qs == null' then do not apply
the dot operator to 'qs'. Nothing weird there.
Your problem is that you took a return value from 'request.getQueryString()'
and didn't check it for 'null' before using it (or used it anyway after
determining that it's 'null').
Assuming that 'request' is of type 'HttpServletRequest', the method
documentation for 'getQueryString' clearly states that it "[r]eturns: a String
containing the query string or null if the URL contains no query string".
Your programming error was not to account for all possible values your code
might receive. When it received such a value, kablooey!
--
Lew