• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with SQL query....

 
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing you have code similar to this:


If object==null, NPE occurs.
 
Ranch Hand
Posts: 225
Spring Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
------------------
 
reply
    Bookmark Topic Watch Topic
  • New Topic