Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
| Search | Today's Posts | Mark Forums Read
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2005, 08:42 AM
Authorized User
 
Join Date: Oct 2004
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default null exception error

hey..


actually i have 19 rows...but sometime i have to insert only two rows values..then null exception error will come...

when i insert the 19 rows values it successfully insert the values in database...and displays values on page..

what now i have to do....


because sometime i have to enter only two rows values...sometimes 5 rows values...


plz give me ur help in this...


my code is as follows:



conn.setAutoCommit(false);

PreparedStatement pstmt = conn.prepareStatement("INSERT INTO gl_mast VALUES (?, ?, ?, ?, ?, ?)");

for(int i=1; i < 20; i++) {

int Code=Integer.parseInt(request.getParameter((i==1)? "code":("code_" +i)));
pstmt.setInt(1,Code);

String Description=request.getParameter((i==1)?"Descripti on":("Description_" +i));
pstmt.setString(2,Description);

float Dr_Amt =Float.parseFloat(request.getParameter((i==1)?"DrA mount":("DrAmount_" +i)));
pstmt.setFloat(3,Dr_Amt );

float Cr_Amt =Float.parseFloat(request.getParameter((i==1)?"CrA mount":("CrAmount_" +i)));
pstmt.setFloat(4,Cr_Amt);

String Type =request.getParameter((i==1)?"type":("type_" +i));
pstmt.setString(5,Type );

float Pct =Float.parseFloat(request.getParameter((i==1)?"pct ":("pct_" +i)));
pstmt.setFloat(6,Pct);

pstmt.addBatch();
conn.commit();
conn.setAutoCommit(true);


}


 
Old January 4th, 2005, 06:10 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Are you sure you mean rows and not columns???
Columns are the number of fields in a database, your code was updating 5 columns.

If this is the case you may still have to pass all filds to your database. If a field is required or madatory in the database you have to poulate it. Also passing null values may create errors. If your databse expects an integer and you pass it something different it will cause an error.

If you were taling about rows, (1 record = 1 row) then use a loop to populate the database the required number of times.

 
Old January 4th, 2005, 06:44 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, I completely misread what you are doing.
I think the problem is that your loop ALWAYS executes 19 times, if you run out of data to pass after two iterations of the loop, then you will get an excception on the third, you cannot update the database with no data!

Try using a for loop with a variable:

for(int i=1; i < x; i++);

Set x to the number of records you need to add.

Alternatively use a while loop that checks the value of fields, i.e. while Code != null

 
Old January 5th, 2005, 02:20 AM
Authorized User
 
Join Date: Oct 2004
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

actually it vary...

sometimes i can insert one record..sometimes 5 record...

up to 19 records..

 
Old January 5th, 2005, 05:26 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you are getting a null exception error it means somewhere a value is expected but you are not passing one, you need to test your variables to ensure they are populated before trying to update.

You could get your program to write out data as it runs to test it, this way you can see what is happening at the point the program failes.





Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript: null or not an object exception ganeshbraj ASP.NET 1.0 and 1.1 Professional 0 September 28th, 2007 08:01 AM
Null reference exception was unhandled labby C# 2005 0 April 24th, 2007 06:14 AM
Null Reference Exception was unhandled labby C# 2005 1 February 9th, 2007 03:15 PM
NULL reference exception Dwizz VB.NET 2002/2003 Basics 3 June 21st, 2005 07:15 AM
Null Exception while reading ajindal Classic ASP XML 3 December 14th, 2004 06:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.