This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes problem with SQL query.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem with SQL query...." Watch "problem with SQL query...." New topic
Author

problem with SQL query....

Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

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


Never try to be a hard-worker. Be a smart-worker.
My Blog
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 02, 2005
Posts: 443
    
    1

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

Joined: Mar 06, 2001
Posts: 13459

I'm guessing you have code similar to this:


If object==null, NPE occurs.
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
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

Joined: May 29, 2008
Posts: 445

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
 
subject: problem with SQL query....
 
Similar Threads
Restore problem
FOREIGN KEY
Problem in paging nation with date format
Problem with date insertion in MySQL
Weird error