hi friends,
seems as if though this null pointers wont let me go peacefully home!!!!
i have a jsp page where i do a get attribute like this--->
[HTML]<%String s24=(String) request.getAttribute("s24"); %>[/HTML]
and to retrieve the value i do some thing like this--->
[HTML]<%=s24.substring(0,9) %>[/HTML]
but the method substring(0,9) is giving me a null pointer exception. have can i avoid or rather eliminate this exception/error?? please guide me regarding this:).
regards,
ajos
11 6692
hi friends,
seems as if though this null pointers wont let me go peacefully home!!!!
i have a jsp page where i do a get attribute like this--->
[HTML]<%String s24=(String) request.getAttribute("s24"); %>[/HTML]
and to retrieve the value i do some thing like this--->
[HTML]<%=s24.substring(0,9) %>[/HTML]
but the method substring(0,9) is giving me a null pointer exception. have can i avoid or rather eliminate this exception/error?? please guide me regarding this:).
regards,
ajos
The string s24 is null. That means the attribute called s24 was never passed to the JSP. Where is it supposed to come from? A servlet?
The string s24 is null. That means the attribute called s24 was never passed to the JSP. Where is it supposed to come from? A servlet?
hi r035198x,
thanks for your reply yet again.wat i did was in my servlet---> -
s24 = resultset.getString(11);
-
httpservletrequest.setAttribute("s24", s24);
-
and in my jsp page the method i mentioned above...so is it not supposed to work if i do a s24.substring(0,9)??
regards,
ajos
hi r035198x,
thanks for your reply yet again.wat i did was in my servlet---> -
s24 = resultset.getString(11);
-
httpservletrequest.setAttribute("s24", s24);
-
and in my jsp page the method i mentioned above...so is it not supposed to work if i do a s24.substring(0,9)??
regards,
ajos
Have a try this... -
s24 = resultset.getString(11);
-
System.out.println(s24); //it must output NULL
-
Debasis Jana
Have a try this... -
s24 = resultset.getString(11);
-
System.out.println(s24); //it must output NULL
-
Debasis Jana
hi Debasis,
where am i supposed to write S.O.Pln ?i want to output this in my jsp page. i tried doing
[HTML]
<%=s24>
[/HTML]
thats printing me null values, but i want to view the substring value instead.
:)
regards,
ajos
hi r035198x,
thanks for your reply yet again.wat i did was in my servlet---> -
s24 = resultset.getString(11);
-
httpservletrequest.setAttribute("s24", s24);
-
and in my jsp page the method i mentioned above...so is it not supposed to work if i do a s24.substring(0,9)??
regards,
ajos
You should understand why the exception is thrown. s24.substring(0, 9) throws a null pointer only if s24 is null. Now looking at your code there is no guarantee that s24 is not null.
Either resultset.getString(11) itself returned null or you did not include the request/response arguments when you opened the JSP from the servlet.
You should understand why the exception thrown. s24.substring(0, 9) throws a null pointer only if s24 is null. Now looking at your code there is no guarantee that s24 is not null.
Either resultset.getString(11) itself returned null or you did not include the request/response argumemnts when you opened the JSP from the servlet.
actually even with <%=s24> i gut a null value coz the query didnt match with requirement, my question is even if with the .substring() method i should get a null value in my results page isnt it? correct me if i am wrong.if thats not how it works then wat changes shud i do??
regards :),
ajos
actually even with <%=s24> i gut a null value coz the query didnt match with requirement, my question is even if with the .substring() method i should get a null value in my results page isnt it? correct me if i am wrong.if thats not how it works then wat changes shud i do??
regards :),
ajos
If you try to derefence null you will get a nullpointer exception so s24.substring(0, 9) does not return null but instead throws a nullpointer exception when s24 is null.
What you should do to make it work depends on how you want it to work. What do you want to do if the value returned from the database is null?
actually even with <%=s24> i gut a null value coz the query didnt match with requirement, my question is even if with the .substring() method i should get a null value in my results page isnt it? correct me if i am wrong.if thats not how it works then wat changes shud i do??
regards :),
ajos
Then try it... -
try{
-
}
-
catch(Exception)
-
{
-
//do nothing
-
}
-
//here the code execution will be normal
-
Debasis Jana
Then try it... -
try{
-
}
-
catch(Exception)
-
{
-
//do nothing
-
}
-
//here the code execution will be normal
-
Debasis Jana
Don't do that. That will hide all the exceptions rather than handle them.
If you try to derefence null you will get a nullpointer exception so s24.substring(0, 9) does not return null but instead throws a nullpointer exception when s24 is null.
What you should do to make it work depends on how you want it to work. What do you want to do if the value returned from the database is null?
with the form beans around even if the result doesnt match with the query we tend to get null as the result...thats wat i want to display if the result of the substring is null.is it possible?
regards,
ajos
with the form beans around even if the result doesnt match with the query we tend to get null as the result...thats wat i want to display if the result of the substring is null.is it possible?
regards,
ajos
That's what the if statement is for. - if(s24 != null) {
-
valueToDisplay = s24.substring(0, 9);
-
}
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by jennifer johnson |
last post: by
|
1 post
views
Thread by Mark |
last post: by
|
5 posts
views
Thread by Tony Cheng |
last post: by
| | | | | | | | | | | | | | | | | |