Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException while inserting data into database

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my project, I have created a functionality which registers Editors.
For this, I have used bean class, jsp, Servlet and a Dao class.

EditorBean.java







EditorServlet



EditorDao.java





Web.xml





AddEditor.jsp






I have a “Editors” table in database with following columns:
SQL> desc editors;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(5)
USERNAME VARCHAR2(30)
PASSWORD VARCHAR2(30)
ADDR VARCHAR2(30)
CNO NUMBER(10)
EMAIL VARCHAR2(30)
AREA VARCHAR2(30)
DOJ DATE

<SQL>




On running the application using tomcat 6, I am getting exception with the following stack trace :
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: null
java.lang.Long.parseLong(Unknown Source)
java.lang.Long.parseLong(Unknown Source)
org.cmtes.Editor.Servlet.EditorServlet.doPost(EditorServlet.java:21)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
________________________________________
Apache Tomcat/6.0.29

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Close all your input tags and retry.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried that. but still, it showing the same error

I am inputing the following data:

Editor's Id : (Successfully Self generated)
UserName: kunal
Address: Samlong
Contact: 1234
E-mail id: abc
Area: Ranchi
Date of joining: (successfully Self generated)


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On your webpage, what's the value of your "t1" textbox ?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"t1" related to my id, which is self generated.

EditorDao





AddEditor.jsp






The id generated is 1001.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean, what is shown on your web browser ? Does the "Editor's Id" textbox show any value ?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it shows 1001
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about "Contact" ("t5") ? Did you enter a value in it ?
 
Eduardo Yañez Parareda
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:
The id generated is 1001.


So rs.getString("ID") is null
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eduardo Yañez Parareda wrote:
Kunal Lakhani wrote:
The id generated is 1001.


So rs.getString("ID") is null

But that is not a problem.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Eduardo Yañez Parareda. That's only generating 1001.


Christophe Verré , yes i am entering values in t5, (value-123)
 
Eduardo Yañez Parareda
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:yes Eduardo Yañez Parareda. That's only generating 1001.


Christophe Verré , yes i am entering values in t5, (value-123)



Print us the correct stacktrace because what you printed doesn't correspond with the source code you posted. Line 21 from EditorServlet is commented out...
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cause for exception is seen at this line: org.cmtes.Editor.Servlet.EditorServlet.doPost(EditorServlet.java:21)

But its is commented out. Can you post the code which caused the exception?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eduardo Yañez Parareda, The stack trace i printed is correct.
 
Eduardo Yañez Parareda
Ranch Hand
Posts: 93
IntelliJ IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:Eduardo Yañez Parareda, The stack trace i printed is correct.


I'm sorry, but it's impossible to have an exception caused in a commented out line...
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eduardo Yañez Parareda wrote:
Kunal Lakhani wrote:Eduardo Yañez Parareda, The stack trace i printed is correct.


I'm sorry, but it's impossible to have an exception caused in a commented out line...


+1 here. Without the correct code we cannot tell the cause for that exception.
 
R Patel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I can see one issue is, your "t1" field is disabled. so whenever your form is getting submit, that field value will not get submit. So i suggest you can take that value under hidden variable. and can change the below line to new as i have mentioned.

existing line:


replace above line with below lines.



Regards,
Ronak.
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but it's impossible to have an exception caused in a commented out line...

The line numbering in this post doesn't neccessary match with the line numbering in the source file. Let's ask Kunal what the exact piece of code is where the exception is thrown.


Kunal, Can you tell us what this piece of code is in your original source file:
EditorServlet.java:21 ?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused friends.

The problem has been solved. What i did is, removed the commented line & closed MyEclipse IDE & started it again.
I don't know what is happening. At present, i am executing the same code, and its running fine. This is the 2nd time i am facing such problem. And if i am changing the code in MyEclipse, to make it to come into effect, i need to restart the IDE, its not getting refreshed while compiling it again

Can anyone tell the reason for same?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, how to replace this scriptlets from EL in my jsp??

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal Lakhani wrote:Also, how to replace this scriptlets from EL in my jsp??



Please start a new thread for a new query
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although you've probably solved your current problem, a hidden bug is ticking in your code. It is lurking in the following line:

Never ever use the INSERT statement without specifying the columns into which you're inserting, for two reasons:

1) If the number of columns in the table is changed, your program suddenly stops working. If you specify the columns, and a new nullable column is added (or column with default value specified), the statement will remain functional. New columns may be added for various reasons (eg. auditing, replication) that are independent of your application.

2) The second reason is much more serious. The order of columns in the table is not necessarily fixed. If the table is recreated for some reason, or column is dropped and added again for any reason imaginable (including inept DB admin), the order of columns might change. If you're lucky, your program crashes with a data conversion error, or breaks an existing constraint. However, your code might continue to work, but insert data into columns different from what is intended. If this is not caught early on, your database might get littered with errorneous data that will be difficult to disentangle.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic