wood burning stoves*
The moose likes Servlets and the fly likes exception while running code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "exception while running code" Watch "exception while running code" New topic
Author

exception while running code

Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

through the following code i am extracting values from database for edit purpose and the following code is calling when i am click on the edit button that is along with every row of table of jsp page



but it is giving following error

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NumberFormatException: For input string: "UserServlet"
java.lang.NumberFormatException.forInputString(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
UserServlet.doPost(UserServlet.java:24)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:402)
org.apache.catalina.servlets.InvokerServlet.doPost(InvokerServlet.java:170)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
InfonetFilter.doFilter(InfonetFilter.java:43)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.18


i don't understand why this error is coming

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

java.lang.NumberFormatException: For input string: "UserServlet"

Is "UserServlet" a number?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Maneesh Godbole wrote:
java.lang.NumberFormatException: For input string: "UserServlet"

Is "UserServlet" a number?


no its a class name
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Ok. What I meant was do you now figure out what is going wrong?
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Maneesh Godbole wrote:Ok. What I meant was do you now figure out what is going wrong?


no.. not yet
Rsudha Ramasamy
Greenhorn

Joined: Mar 26, 2012
Posts: 4
the following line causes this exception

Integer.parseInt(str[3]);

try printing str[3]
i guess it's something thats not an integer


SCJP,SCWCD
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Rsudha Ramasamy wrote:the following line causes this exception

Integer.parseInt(str[3]);

try printing str[3]
i guess it's something thats not an integer


i'm trying to edit particular record on the basis of P_ID and it is a integer type.
i provide that in the database
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Maybe.
But what you are getting from
String str[] = request.getRequestURI().toString().split("/");
int P_ID = Integer.parseInt(str[3]);

is not an integer.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Maneesh Godbole wrote:Maybe.
But what you are getting from
String str[] = request.getRequestURI().toString().split("/");
int P_ID = Integer.parseInt(str[3]);

is not an integer.


so can you please tell me what changes i have to make so that it will not show this exception.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Like you were told before, try printing what actual values you are getting.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Maneesh Godbole wrote:Like you were told before, try printing what actual values you are getting.


but actual values i'm getting in string through session from the previous page but i have to convert it into the integer as in database that is stored in the form of integer and query will work only when i match the field of integer type.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

It does not really matter what you wish to do with the value.
Point is the string to int logic is failing because the string cannot be converted to an int.
So do you want to share what it prints?
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Maneesh Godbole wrote:It does not really matter what you wish to do with the value.
Point is the string to int logic is failing because the string cannot be converted to an int.
So do you want to share what it prints?


My first jsp page is following

and when i click on edit button then it should redirect me to follwing servlet


and after it should take me to following jsp page

on this page fetched value should be shown
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Maneesh Godbole wrote:
So do you want to share what it prints?

Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

what i think. In code:


str[3] is null. so check these by printing the value of str[3]. if it is null or not.


Correct Me if i am wrong
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 532

The exception clearly tells, that it cannot convert the String value to an Integer. That means check what is been passed to Integer.parseInt, it is not an number.
As suggested earlier by Maneesh and Rsudha, why can't you just print that value and show us what it prints?
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Prasad Krishnegowda wrote:The exception clearly tells, that it cannot convert the String value to an Integer. That means check what is been passed to Integer.parseInt, it is not an number.
As suggested earlier by Maneesh and Rsudha, why can't you just print that value and show us what it prints?


now i change the code but stll it is not redirecting me to jsp

and my below code that is for delete that is working which is following and redirecting me to other page

Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 532

I am just wondering, is it so difficult to do what we all suggested here (just printing one value and posting here the result..)

I request for the last time. Please post the value of the variable been passed to Integer.parseInt() as suggested by Maneesh,Rsudha and Neeraj already.

If you just keep on posting code, without doing what's suggested, its of no use.
Also, posting things like, below code is working and below is not working, will not help. You are expecting us to keep on guessing what changes you did?
You need to TellTheDetails, such as what changes you did, what you expected and what came..

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

If the value is coming as a query string, you can simply use request.getParameter to get its value...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Ankit Garg wrote:If the value is coming as a query string, you can simply use request.getParameter to get its value...


when i am passing the the value below like this and disable to bring variable from the previous page then it is giving below output that is what i want



but i have to work generaly on that it is not working

i have attached the output screen shot that is based on fix value.

and following is syntax for calling from the previous page to this servlet page , the event which has to be performed on button click
function editRecord(P_ID){

window.location.href="../edit/UserServlet?"+P_ID;
}




[Thumbnail for New Picture.png]

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Are you sure that the following path is correct?

String nextJSP = "../jsp/editUser.jsp";


Swastik
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Swastik Dey wrote:Are you sure that the following path is correct?

String nextJSP = "../jsp/editUser.jsp";


as i told it is working on fix value but not on query string and the screen shot i attached that is coming because i have given the proper path.
that's for sure prob is not in the path there is something in the query string
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Does exception stack trace say something?
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi,

Why can't you use query parameter instead of query string.
You can try with following code changes.
JavaScript



Java Code




Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Swastik Dey wrote:Does exception stack trace say something?


No
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Piyush Mangal wrote:Hi,

Why can't you use query parameter instead of query string.
You can try with following code changes.
JavaScript



Java Code






ok then where should i declare userId?
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
You are already defining userId in your js function (see below). What we are doing here is P_ID will be passed as a request parameter with the name "userId" which we get by using request.getParameter("userId") in servlet.




Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Megha Singhal wrote:
Piyush Mangal wrote:Hi,

Why can't you use query parameter instead of query string.
You can try with following code changes.
JavaScript



Java Code






ok then where should i declare userId?


now this is giving following error

UserServlet.java:27: cannot find symbol
symbol : method getRequestParameter(java.lang.String)
location: interface javax.servlet.http.HttpServletRequest
String str = request.getRequestParameter("userId");
^



Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
My Apologies for typo.

It should be String str = request.getParameter("userId");
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Piyush Mangal wrote:My Apologies for typo.

It should be String str = request.getParameter("userId");


but the problem still same .
i'm still not able to redirect to jsp page actualy this servlet will run only when i connect it with previous jsp page as query string is passing from jsp to servlet page and then i am redirecting it to the next jsp page so the problem is still same i'm not able to redirect to next jsp page.
my main problem is this only that it is not going on next page and there is no error in the path as this path is working on another but i'm not understanding why it is not working on this page.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Can you describe the structure of your web application including jsp folder.

Please also share the source code for
1. JSP from where you are taking user to Servlet.
2. The servlet which will forward the request to Jsp

Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Piyush Mangal wrote:Can you describe the structure of your web application including jsp folder.

Please also share the source code for
1. JSP from where you are taking user to Servlet.
2. The servlet which will forward the request to Jsp



i already mentioned those servlet and jsp above but i am mentioning again.

below is the first jsp page

below is the servlet on which the jsp page will redirect on edit button click

below is the jsp code on which servlet should redirect

so above are the required servlet and jsps







Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

ok. so you are not redirecting to another page then what is the error message or in which error page you are redirecting or something else.
is there any exception?
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Neeraj Dhiman wrote:ok. so you are not redirecting to another page then what is the error message or in which error page you are redirecting or something else.
is there any exception?


yes this is redirecting to my college main page.
it is in our server if there is any error in code then it will redirect to college main page without showing any exception or error
Neeraj Dhiman
Ranch Hand

Joined: Dec 19, 2011
Posts: 63

it mean there is errors in your code. have you checked your console for exception? your are using e.printStackTrace();
try to find in which segment of code you got exception which will caused to redirect to another page.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

In you jsp you have something like. What does this mean?
request.getAttribute("userList")!=""

You probably want to make sure that the list not empty?


Problem 2, you are initializing the arraylist inside the loop


So what happens in the above code, every time the loop executes a new arraylist object is created, means it will always store only the last record in the array list. Initialize it before you start the loop.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Hi,

Why are you using ?P_ID="+P_ID and not ?userId="+P_ID in your javascript?




If you want to use ?P_ID="+P_ID , then in your code also , you will need to change request parameter name accordingly.
Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 225

Piyush Mangal wrote:Hi,

Why are you using ?P_ID="+P_ID and not ?userId="+P_ID in your javascript?




If you want to use ?P_ID="+P_ID , then in your code also , you will need to change request parameter name accordingly.


in my jsp P_ID is defined so i have to use P_ID and i alraedy made changes in req.getparameter but still problem is coming
 
GeeCON Prague 2014
 
subject: exception while running code