0

i am trying to insert these values into the table in the database, all values line up and are correctly spelt and ordered, this code has been working and i cannot for the life of me figure out why it is not now. probably something silly but ive been over and over it with a fine tooth comb. all variables have values.

    public void addNewProduct(int uuid, String location, String product, String category, int items, int numberOfBoxes, double price, String supplier, String value) throws SQLException {

    try {

            dbCon.NSStock();

            dbAction = "INSERT INTO StockList (uuid, location, product, category, items, numberOfBoxes, price, supplier, value) VALUES (?,?,?,?,?,?,?,?,?)";

            ps = dbCon.NSStock().prepareStatement(dbAction);


            if(uuid == 0) {

                uuid = createUUID();

            }

            ps.setInt(1, uuid);
            ps.setString(2, location);
            ps.setString(3, product);
            ps.setString(4, category);
            ps.setInt(5, items);
            ps.setInt(6, numberOfBoxes);
            ps.setDouble(7, price);
            ps.setString(8, supplier);
            ps.setString(9, value);

            ps.executeUpdate();

            JOptionPane.showMessageDialog(null, "Stock List Updated");

    }catch(SQLException e) {

        e.printStackTrace();

    }finally{

        dbCon.NSStock().close();

    }   
}

//exception details

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:130)
at org.sqlite.jdbc3.JDBC3PreparedStatement.setInt(JDBC3PreparedStatement.java:329)
at nightshelter.Stock.addNewProduct(Stock.java:84)
at nightshelter.NightShelterGUI.addNewProductBTNActionPerformed(NightShelterGUI.java:2587)
at nightshelter.NightShelterGUI.access$000(NightShelterGUI.java:27)
at nightshelter.NightShelterGUI$1.actionPerformed(NightShelterGUI.java:487)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
phill
  • 1
  • 4
  • Please post the _complete_ stack trace and identify the line in your code that caused the exception. – Jim Garrison Jun 10 '20 at 17:36
  • And which is line #84? `ps.setInt(1, uuid);`? – Alex Rudenko Jun 10 '20 at 17:42
  • The problem is in `Stock.addNewProduct`; not in the code which you have posted. – Arvind Kumar Avinash Jun 10 '20 at 17:46
  • which or what? it is the first setting statement under the if statement , basically it is a unique id that is randomly generated if the product is being added new, for the reason when products are returned from the shop back to the stock list in the warehouse without all being sold, instead of creating a new uuid it keeps the old one from when the products were sent out to the shop, just for concurrency really if people remember the uuid then you dont want it changing every time as it can be used to search products instead of writing the products full name. – phill Jun 10 '20 at 17:47
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Arvind Kumar Avinash Jun 10 '20 at 17:48
  • never mind just had a little brain wave, i moved the if statement out of the try/catch and now it works. i knew it would be silly! – phill Jun 10 '20 at 17:50
  • This seems to be a bug in the sqlite JDBC driver, as a JDBC method should throw a `SQLException` if something is wrong. You may want to check if there is a newer version available, and otherwise report a bug. – Mark Rotteveel Jun 11 '20 at 15:44
  • thanks Mark ill do that now – phill Jun 11 '20 at 19:05
  • looks like i have the latest, sqlite-jdbc-3.30.1.jar – phill Jun 11 '20 at 19:11
  • it must be something else as i keep getting a JNI error from the jar file, even though ive updated both jdk and jre both are 14.0.1 and ive changed the package name as both came up as solutions to the problem online, ive also updated netbeans and it still is doing it, yet it works on another computer...go figure – phill Jun 12 '20 at 00:36

1 Answers1

0

i moved the if statement out of the try/catch and now it works.

phill
  • 1
  • 4