473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 7029
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

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

Similar topics

1
by: jennifer johnson | last post by:
Hello All, I appreciate anyone's assistance. I wanted to validate user customizable field values so I changed my JSP page so that the form action would post existing form - action=<%=...
1
by: Mark | last post by:
Hi, I'm writing user controls with custom events. I have a parent custom event that exposes some abstract methods and some custom events. I have also created some new user controls that...
5
by: Tony Cheng | last post by:
for (int i=0; i<_request.Form.Count; i++ ) { string key = _request.Form.GetKey(i); if ( !key.Equals("formCode") && !key.Equals("formLanguage") && !key.Equals("__VIEWSTATE") &&...
3
by: chramprasad | last post by:
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ...
1
by: zahidkhan | last post by:
Hi All, Plz help me if you can..... I have a program something like this int main(int argc,char* argv) { try { int* p = NULL;
1
by: MRamaLakshmi | last post by:
hi, I am developing an application using Java Applet which will be uploading files. Its throwing Null Pointer exception while detecting the proxy when we are trying to load the applet using Java6....
1
by: bansalrichaacc | last post by:
In The Following Code I am Getting Null Pointer Exception try{ ClientBean clientInfo = clientManager.getClientList(); System.out.println("richa2"+clientInfo); if (clientInfo != null )...
5
by: iamdennisthomas | last post by:
Hi Guys i was developing a action servlet which is actulally getting the data from a form and putting it in the db but while executing i am getting a null pointer exception /* * Generated by...
6
by: swathi amireddy | last post by:
Currently im trying to migrate a project from .net 1.1 to 2.0.While running the application its giving an exception for the below code while doing the update operation: protected void...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.