Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with SQL query....

 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String SQL="INSERT INTO tbl_rmadetails (date,rma_no,customer_id,item_id,customer_inv_no,customer_inv_date,customer_inv_amount,serial_no,fault_desc,replaced_serial_no,status,vendor_id,vendor_invoice_no,vendor_inv_date,vendor_rma_ref,remarks) VALUES(str_to_date(da,'%m/%d/%Y'),?,cust,ite,?, str_to_date(date1,'%m/%d/%Y'),?,?,fdesc,?,stat,?,?,?,?,?)";

Hi friends.. i use the above syntax to insert some values into Database from my JSP page. when i execute this it shows java.sql.NullPointerException.. The problem is three of the field(item_id,customer_id,vendor_id) is foreign key where in my form i enter only 2 of these fields ie) i leave one foreign key field empty. Can i do this? If not then suggest me some other options.. Any help would be appreciated
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A NullPointerException will be the effect, the cause is trying to enter invalid code.

Yes, you can leave fields, including foreign keys null, as long as it makes sense in the database. You may want to skip setting a value on these fields since the problem may be casued by the null field itself.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I dont understand is how can Java throw a NullPointerException because a foreign key is null in the SQL query? The database can throw an error when NULL is inserted in a NOT NULL column.

Can't you not insert the row into the table where the foreign key comes from and then fetch it so that you have the foreign key value when you try to execute this query.

Are you sure where the exception is coming from?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing you have code similar to this:


If object==null, NPE occurs.
 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajkumar,

After analysing your query, it seems you are trying to insert a null value from Jsp to Database.(Same as you said).
See David's comment carefully
If you are sure that one field(Foreign key column) would be empty, then avoid that column from your Insert statement.It will automatically add the null value. Don't try to insert that from Jsp.


Regards
Baseet Ahmed
##############
Value of Parents
Jannat(Heaven) is under the feet of Mother.
Father is the middle gate of Jannat(Heaven).

[ ISLAM ]
##############
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I analyze the problem and try to insert the foreign key values by loading those foreign key values in the combo box of that corresponding JSP page. But even now i have a NPE that for the primary key field. How do insert a primary key field without getting any input from user. I try the following code but it not works fine..
Here is my code
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic