• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NumberFormatException: null

 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello! i'm trying to run a jsp code but i get the following error:
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source)

the code is this:


what may be the problem?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem #1: Having Java code in your JSP. That's an obsolete and outdated practice from over a dozen years ago. Why are you still doing it? Why is this code not in a servlet?

Problem #2: id is likely null.

(Problem #3 is creating connections in the web layer rather than the model -- but that's another topic.)
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but for what reason is it null because it has a value in database
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
id has nothing at all to do with the database. You are obtaining it from a request parameter.
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean i am gettin it from here :
<td>
<a href="cancelRruge.jsp?id=<%=rs.getInt(1)%>">Mbyll</a>
</td>
and rs.getInt(1) is getting a value from database
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you sure? Did you view the generated HTML? Is the request parameter correct as viewed in the URL in the browser after you click the link? Don't make us guess what you've already done -- tell us.

Also you never answered my questions about putting the code in a JSP. Is this an old legacy page, or new code?
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://localhost:8080/Parkimi/cancelRruge.jsp?id=1
i am new in jsp and i dont know how to work with servlets. i need to do a school project, that's why i am usin jsp
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and yes id is 1 in database
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
laura xhaferaj wrote:i need to do a school project, that's why i am usin jsp

That's the sort of thing you should let us know up-front. Otherwise, we think you are doing real code and will wonder why you are doing things the way that you are.

For the future, be aware that putting Java code into a JSP is akin to trying to use vacuum tubes in a TV set.

The error message is clearly stating that null is being passed to parseInt(). I would suggest putting a logging statement into the code to verify this.
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your advice :) about the logging statement what do you mean? sorry but i am really new at this :/
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and what is the most interesting thing is that while i am trying do execute a another code in jsp that makes a similar thing it shows me this error and before didn't , it worked before! why ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the simplest form of logging is just to blat out something to System.out. Using java.util.logging or (the vastly superior) Log4J package is more sophisticated.

For example:
That will emit a line to the console that you can inspect and see what actual value the JSP thinks it has.

By the way, using id to represent the string version, and ID to represent the numeric version is a bad, bad idea. It's confusing at best, and starting a variable name with a capital letter defies convention.

Choose better names.
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
org.apache.jasper.JasperException: An exception occurred processing JSP page /cancelRruge.jsp at line 19

16: <%
17: String id = request.getParameter("id");
18: System.out.println("The value of id is " + id);
19: int ID = Integer.parseInt(id);


Error is the same. And nothing else is shown. i don't understand why it doesn't work now, i made an other project before and it was executed without any error and now not :/
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
laura xhaferaj wrote:Error is the same.

Of course it is. Logging isn't going to change the value. It's still null.

And nothing else is shown.

Then you're not looking in the right place. What OS and server are you using? If Windows and Tomcat, the output will appear in the DOS console window for Tomcat. If you're using a real OS, it'll be in Tomcat's catalina.out file, which you can tail.

i don't understand why it doesn't work now, i made an other project before and it was executed without any error and now not :/

Obviously something is awry. Verifying that you have a null there is the 1st step to finding out why.
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of id is 4

it prints the value of id. i used a try catch instead!
 
laura xhaferaj
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried even this


and got the result: The value of id is 6
which means that ID is not null. but i dont understand why it says that is null
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your code is reaching the logging statement, then it is not the previous statement that is throwing the exception.

Perhaps you should post the complete JSP and the stack trace. I don't see anywhere else where you are calling parseInt().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!