I am using H2 databases to store the information for my application. After creating a sale in the sale screen, I click a button that says 'Complete Sale'. Upon doing so, all of the information is added to the database. A 'for' loop iterates through my table and gathers various information from the sale screen to add a different row for each item that the customer purchased. Now, here is my problem:
When I create a sale of three items, four rows are added to the database, and the fourth row is filled with null values. It must be a problem with my 'for' loop, but I'm not sure what the problem is.
I have an integer 'RowCount' that determines how many rows are in the table (how many items). Then, my 'for' loop runs while integer 'i' (declared variable) is less than 'RowCount'. Here is my code:
//Get tax rate
String TaxRate = "";
try {
Class.forName("org.h2.Driver");
Connection connection = DriverManager.getConnection("jdbc:h2:./RetailApplicationDatabase;AUTO_SERVER=TRUE");
String sql;
Statement stmt;
sql = "SELECT TaxRate FROM SETTINGS WHERE OnOff = 'ON'";
stmt = connection.createStatement();
ResultSet results = stmt.executeQuery(sql);
while (results.next()) {
TaxRate = results.getString("TaxRate");
}
} catch (ClassNotFoundException | SQLException ex) {
JTextArea ErrorMessage = new JTextArea("Error: " + ex, 6, 40);
ErrorMessage.setWrapStyleWord(true);
ErrorMessage.setLineWrap(true);
JScrollPane ErrorPane = new JScrollPane(ErrorMessage, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ErrorPane.setPreferredSize(new Dimension(400, 200));
JOptionPane.showMessageDialog(null, ErrorPane, "Error", JOptionPane.ERROR_MESSAGE);
}
double TaxRateAmount = Double.parseDouble(TaxRate);
DefaultTableModel model = (DefaultTableModel) SaleItemTable.getModel();
int RowCount = model.getRowCount();
String TicketNo;
String CustomerID;
String InventoryNo;
String Description;
String Cost;
String Retail;
String IndividualSubTotal;
String IndividualTax = "";
String IndividualTotal = "";
String SaleSubTotal;
String SaleTax;
String SaleTotal;
for(int i = 0; i < RowCount; i++) {
TicketNo = TicketNoNumberLabel.getText();
CustomerID = CustomerIDLabel.getText().substring(12);
InventoryNo = String.valueOf(SaleItemTable.getValueAt(i, 0));
Description = String.valueOf(SaleItemTable.getValueAt(i, 1));
Cost = String.valueOf(SaleItemTable.getValueAt(i, 2));
Retail = String.valueOf(SaleItemTable.getValueAt(i, 3));
IndividualSubTotal = String.valueOf(SaleItemTable.getValueAt(i, 3));
double dIndividualSubTotal = Double.parseDouble(IndividualSubTotal);
double dIndividualTax = dIndividualSubTotal * TaxRateAmount;
DecimalFormat df = new DecimalFormat("#.00");
String sIndividualTax = df.format(dIndividualTax);
double dIndividualTotal = dIndividualSubTotal + dIndividualTax;
String sIndividualTotal = df.format(dIndividualTotal);
SaleSubTotal = SubTotalAmountLabel.getText();
SaleTax = TaxAmountLabel.getText();
SaleTotal = AmountDueAmountLabel.getText();
DatabaseRecords SaveSale = new DatabaseRecords();
try {
SaveSale.SaveSale(TicketNo, CustomerID, InventoryNo, Description, Cost, Retail, IndividualSubTotal, sIndividualTax, sIndividualTotal, SaleSubTotal, SaleTax, SaleTotal);
} catch (ClassNotFoundException | SQLException ex) {
JTextArea ErrorMessage = new JTextArea("Error: " + ex, 6, 40);
ErrorMessage.setWrapStyleWord(true);
ErrorMessage.setLineWrap(true);
JScrollPane ErrorPane = new JScrollPane(ErrorMessage, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ErrorPane.setPreferredSize(new Dimension(400, 200));
JOptionPane.showMessageDialog(null, ErrorPane, "Error", JOptionPane.ERROR_MESSAGE);
}
}
DatabaseRecords SaveSale = new DatabaseRecords();
try {
SaveSale.SaveSale(null, null, null, null, null, null, null, null, null, null, null, null);
} catch (ClassNotFoundException | SQLException ex) {
System.out.println("Exception: " + ex);
}
I tried to change the 'for' loop code to for(int i = -1; i < RowCount; i++), and that returned an ArrayIndexOutOfBoundsException, which makes sense. So I tried for(int i = 1; i < RowCount; i++) and for(int i = 0; i < RowCount - 1; i++), which both excluded one row of data.
If anyone can help me understand where I am going wrong, that would be great! I assume it's a simple fix... I just can't figure it out, and I don't want to fill the database with a bunch of null values.
Thank you in advance for any helpful answers.
Note: I can't post the image of my data table, because I do not have enough reputation points. However, you can imagine that it is simply inserting one row of null values to the data table. For what it's worth, this row of null values is the LAST row that is being added to the data table.