Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException when running a JSP

 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, due to confusion on my previous thread, I'm starting a new one. So, I have a function called getPeopleByLastName(String last_name) and I'm calling it in a servlet (below). It is supposed to retrieve the value from DB and put it into the <c: out> tag for firstName. However I receive the following exception


This is my JSP - which is supposed to retrieve input from the people.lastName and display the name retrieved from the DB in people.firstName (all processed by the servlet which calls the getPeopleByLastName function in another class


This is my servlet


And here is my constructor for Person class...not sure if it helps or not



This might help as well...it's the code for the search function - the select statement is not shown..but there's nothing wrong with it

I'd appreciate any help
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I'd like to note is that if I have the following code below - everything works...however, it only works for the first person in the database with the last name entered in the search criteria. If there is a second person with the same name, it will not get displayed. And I want all of the people with the same to be displayed, not just the first one that the program encounters....and for that I am thinking I need some sort of a loop...?


 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the problem is in the code you posted.
From the message i see, you are trying to handle the String "last_name" as if it was a number. Are you trying to convert to number some String? if yes, probably there the exception is thrown.

Can you post the stacktrace printed?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please continue in the original thread. I have closed that one as you have started this one. But please, next time, just keep things to one topic.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't see anything that can cause a conversion error, but it's senseless to focus on that when the posted code is so clearly broken.

You are setting a list as a scoped variable, and then treating it like a bean on the JSP page. That obviously won't work. If you want to show the entries in the list, you need to iterate over the list with <c:forEach>
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I really appreciate all of the help with this issue...To answer the questions, no I am not doing any number conversions to String anywhere. The field is also set to varchar in the DB. So, considering that, I think it has to do with the number of entry in the list?

So, considering that the following code is ok, I've tried to do a loop inside the JSP page



And here is a loop in my JSP, trying to iterate through the Person list



And here is the full exception

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still find this really difficult to believe.

How about instead of posting fragments of the JSP you post the entire thing? Because "lastName" isn't remotely involved in the JSP fragment you say causes the error:By the way, "personlist" is a crappy name for something holding a single person.

You're saying if you take this code out of the JSP you no longer get the exception?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,then you could post the jsp, there's a subtle mistake, so evident that you can't even imagine to see it.
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, below is my whole JSP page. I appreciate all of your help. This problem has been drivivng me insane for the past week. I've spent so many hours on it. Could it be a problem with the conversion of firstName to a list index??



Here is my servlet again
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help if you indented your JSP properly. Unindented code is very hard to inspect.
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i will do that
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you done to try and isolate the problem? Comment out the elements in the JSP and start adding them back a bit at a time. That can help isolate the specific element that's triggering the issue.
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I said before, if I replace the following code


With this


it works fine, however it only gets the first person with the specified last name from the database. if there is another person with the same last name, it does not display him.

in the JSP i simply have this

So far, this leads me to believe that there is something wrong with the index of the Person list and I am doing something wrong in assigning it using a <c:ForEach> loop...All that I'm trying to do is receive text input from the JSP, insert that input in the search function inside the servlet and return the value of first name in a person list somehow...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If people isn't a single person, then ${person.lastName} is trying to apply the "lastName" part to a list--that obviously won't work. You need to either send a list, or send a single person, and make sure the JSP code matches with the Java.

In the code fragmentyou're attempting to treat the same variable ("people") as both a single person and as a list.

"personlist" is still a silly name for something that's actually a single person. How about... "person".
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping, that by following my directions, that he would have been able to pick that out himself. You can lead a horse...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was getting frustrated.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it didn't seem like he was going to follow my directions in any case. So no foul.
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still receiving the same NumberFormatException even if I change it to what you have said...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er... I think you missed the actual important part.
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I figured it out....if I get rid of value="${people.lastName} it works...I understand it now.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got it worked out. It's not so much about removing that line, it's about keeping track of what you're passing around. You can still pass the value, just not in the way you were trying to do it. (And it's important to include the necessary information when asking questions, particularly when multiple people point out that the code you posted couldn't possibly cause the error! :)
 
dekij ra
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I really appreciate the help. Thanks once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic