469,271 Members | 1,435 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

substring(0,9) giving me null pointer exception

283 100+
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
Oct 10 '07 #1
11 6451
r035198x
13,262 8TB
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?
Oct 10 '07 #2
ajos
283 100+
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--->
Expand|Select|Wrap|Line Numbers
  1. s24 = resultset.getString(11);
  2. httpservletrequest.setAttribute("s24", s24);
  3.  
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
Oct 10 '07 #3
dmjpro
2,476 2GB
hi r035198x,
thanks for your reply yet again.wat i did was in my servlet--->
Expand|Select|Wrap|Line Numbers
  1. s24 = resultset.getString(11);
  2. httpservletrequest.setAttribute("s24", s24);
  3.  
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...
Expand|Select|Wrap|Line Numbers
  1. s24 = resultset.getString(11);
  2. System.out.println(s24); //it must output NULL
  3.  
Debasis Jana
Oct 10 '07 #4
ajos
283 100+
Have a try this...
Expand|Select|Wrap|Line Numbers
  1. s24 = resultset.getString(11);
  2. System.out.println(s24); //it must output NULL
  3.  
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
Oct 10 '07 #5
r035198x
13,262 8TB
hi r035198x,
thanks for your reply yet again.wat i did was in my servlet--->
Expand|Select|Wrap|Line Numbers
  1. s24 = resultset.getString(11);
  2. httpservletrequest.setAttribute("s24", s24);
  3.  
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.
Oct 10 '07 #6
ajos
283 100+
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
Oct 10 '07 #7
r035198x
13,262 8TB
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?
Oct 10 '07 #8
dmjpro
2,476 2GB
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...

Expand|Select|Wrap|Line Numbers
  1. try{
  2. }
  3. catch(Exception)
  4. {
  5.  //do nothing
  6. }
  7. //here the code execution will be normal
  8.  
Debasis Jana
Oct 10 '07 #9
r035198x
13,262 8TB
Then try it...

Expand|Select|Wrap|Line Numbers
  1. try{
  2. }
  3. catch(Exception)
  4. {
  5.  //do nothing
  6. }
  7. //here the code execution will be normal
  8.  
Debasis Jana
Don't do that. That will hide all the exceptions rather than handle them.
Oct 10 '07 #10
ajos
283 100+
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
Oct 10 '07 #11
r035198x
13,262 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. if(s24 != null) {
  2.     valueToDisplay = s24.substring(0, 9);
  3. }
Oct 10 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Tony Cheng | last post: by
3 posts views Thread by chramprasad | last post: by
1 post views Thread by bansalrichaacc | last post: by
5 posts views Thread by iamdennisthomas | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.