my dog learned polymorphism*
The moose likes JDBC and the fly likes Problem in updating the fields to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem in updating the fields to database" Watch "Problem in updating the fields to database" New topic
Author

Problem in updating the fields to database

lisy jessica
Ranch Hand

Joined: Oct 27, 2010
Posts: 47
Hi

I am trying to display records then onclick edit button the fileds will edit then update button , onclick this button the database shd update with the fields, I am getting NullPointer Exception, Dont knw wats the problem, Here is my code

OutransEdit.java



The Dt_of_OUT_Transaction column is VARCHAR2(20).


This code is giving me NullPointer Exception, Can anyone please look into my code and correct, Its urgent. I really dont knw where is the problem. Please help me out.

Thanks in advance
Lissy.
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

This seems like it belongs better in the JDBC forum. With that being said, I have a few comments about your code.

When you are asking people to help solve a problem, it is nice and sometime necessary to provide the stack trace. There are a lot of things in your code that could be causing errors. The most likely problem is the is a problem with your setting of values from your request. Request parameters are ALWAYS Strings. Your use of toString() could be causing the NullPointer if the parameter does not exist. Have you verified that all the request parameters are being set? Try writing all the Strings to a log to see what their values are being set as.

Whenever you are performing a database update, ALWAYS USE java.sql.PreparedStatement. Using regular java.sql.Statement leaves your application open for SQL injection attacks. You may not want to have your request parameters exactly match the names of the fields in your table. That makes it even easier to perform the SQL injection. You should also sanitize any inputs for not only SQL injection, but also to prevent simple formatting issues. Do you verify that all the request parameters are of the proper type and length? Do any varchars exceed the length of the database field?

Your database structure should also be in the Third Normal Form. The way you have your table structured defeats the purpose of having a database since it acts more like a spreadsheet. Is there a reason that your Inventory_Qty field is stored as a varchar instead of a numeric type?

A minor coding point, but a rule of thumb is not to have a single line of code exceed the width of the screen. You should add some line breaks into your SQL query string.


OCJP6, OCWCD5
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Which line is showing npe? And it would be better to use PreparedStatement instead of Statement.


Swastik
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Moved to the JDBC forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Swastik Dey wrote:Which line is showing npe?


I predict it's going to be one of the lines like

where request.getParameter("Inventory_Qty") is returning null (because the user didn't fill in that box in the HTML form).
lisy jessica
Ranch Hand

Joined: Oct 27, 2010
Posts: 47
Thanks alot for the quick responses,

The full stack trace is




I am still getting the same error, The problem was with Inventory_Qty, The datatype of Inventory_Qty is Number, now I removed this Inventory_Qty from my code and trying to update other fields using prepared statement, am still getting the above error. Please help me out. I am really confused y am getting still getting the error.

Thanks in advance
Lissy.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Well the exception says it is happening on line 31 of that class:
java.lang.NullPointerException
at update.OutEditData.doPost(OutEditData.java:31)
which in the code you posted is this line: Have you followed Mike's advice above and printed out the value of each of the parameters (and also not called the toString() method) like:
Also another code style thing, variables typically start with a lower case letter to make it easier for someone reading the code to know what is a class and what is a variable.

Sean

I love this place!
lisy jessica
Ranch Hand

Joined: Oct 27, 2010
Posts: 47
Thank you so much for the prompt reply, will try this and get back to you.


Thanks,
Lissy.
lisy jessica
Ranch Hand

Joined: Oct 27, 2010
Posts: 47
I have printed the values, two values are returning null thats y am getting NullPointerException, the values are Requested_By, Issued_By

this is my EditServlet.java



OutEditData.jsp




I dont know y the two values are null, Requested_By and Issued_By, Please can anyone look into my code and correct me.

Thanks in advance
Lissy.
lisy jessica
Ranch Hand

Joined: Oct 27, 2010
Posts: 47
Thanks alot everyone for helping me, I got it finally, I made a silly mistake, I gave a space after the Requested_By and Issued_By that's y am getting NPE.

String Requested_By = request.getParameter("Requested_By ");

String Issued_By = request.getParameter("Issued_By ");


Thanks alot everypone once again for helping me.

Thanks
Lissy.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in updating the fields to database
 
Similar Threads
alert in servlet with 'Are you sure you want to delete?'
How to Export web page(jsp page) to excel using jsp or servlets
Problem in updating data to Oracle database in servlets
How to prompt if there is no match in database using servlet?
Problem in inserting data into oracle db from jsp