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

Problem in updating the fields to database

 
lisy jessica
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Fedora Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line is showing npe? And it would be better to use PreparedStatement instead of Statement.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
lisy jessica
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for the prompt reply, will try this and get back to you.


Thanks,
Lissy.
 
lisy jessica
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic