简体   繁体   中英

ArrayIndexOutOfBoundsException while using prepared statement INSERT INTO

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM