0

プログラムで次のエラーが発生します。

run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementData(Vector.java:730)
    at java.util.Vector.elementAt(Vector.java:473)
    at javax.swing.DefaultListModel.elementAt(DefaultListModel.java:266)
    at bfpl.BFPL$1$2$13.actionPerformed(BFPL.java:851)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

私のコードは次のとおりです。

final JFrame f8=new JFrame("New Schedule");
                                    f8.setVisible(true);
                                    f3.setVisible(false);
                                    f8.setSize(1000,1000);
                                    f8.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                    final JPanel p8=new JPanel(new GridBagLayout());
                                    GridBagConstraints g8=new GridBagConstraints();
                                    g8.insets=new Insets(2,2,2,2);
                                    JButton b21=new JButton("Back");
                                    JButton b22=new JButton("Save");
                                    JLabel l40=new JLabel("Match No");
                                    JLabel l41=new JLabel("Home Team");
                                    JLabel l42=new JLabel("Away Team");
                                    JLabel l43=new JLabel("Stadium");
                                    JLabel l44=new JLabel("Date");
                                    JLabel l45=new JLabel("mm/dd/yyyy");
                                    JLabel l56=new JLabel("City");
                                    final JTextField t38=new JTextField(15);
                                    JTextField t39=new JTextField(15);
                                    DefaultListModel dl1=new DefaultListModel();
                                    DefaultListModel dl2=new DefaultListModel();
                                    DefaultListModel dl3=new DefaultListModel();
                                    DefaultListModel dl9=new DefaultListModel();
                                    String query1="Select Stadium,Location from Stadiums";
                                    try{
                                        try {
                                            Class.forName("com.mysql.jdbc.Driver");
                                        } catch (ClassNotFoundException ex) {
                                            Logger.getLogger(BFPL.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                        Connection con1=DriverManager.getConnection("jdbc:mysql://127.0.0.1/BFPL","root","ilovepepsi");
                                        Statement stm=(Statement) con1.createStatement();
                                        ResultSet rs=stm.executeQuery(query1);
                                        while(rs.next()){
                                            String std=rs.getString("Stadium");
                                            String std1=rs.getString("Location");
                                            dl1.addElement(std);
                                            dl9.addElement(std1);
                                        }

                                    }   
                                    catch(SQLException ae){
                                        System.out.println("SQLException:"+ae.getMessage());
                                        System.out.println("SQLState:"+ae.getSQLState());
                                        System.out.println("VendorError:"+ae.getErrorCode());
                                        JOptionPane.showMessageDialog(p8,"Error in submitting data!");
                                    }
                                    String query2="Select Team_Name from Team";
                                    try{
                                        Connection con2=DriverManager.getConnection("jdbc:mysql://127.0.0.1/BFPL","root","ilovepepsi");
                                        Statement stm1=con2.createStatement();
                                        ResultSet rs1=stm1.executeQuery(query2);
                                        while(rs1.next()){
                                            String t=rs1.getString("Team_Name");
                                            dl2.addElement(t);
                                            dl3.addElement(t);
                                        }
                                    }
                                    catch(SQLException ae){
                                        System.out.println("SQLException:"+ae.getMessage());
                                        System.out.println("SQLState:"+ae.getSQLState());
                                        System.out.println("VendorError:"+ae.getErrorCode());
                                        JOptionPane.showMessageDialog(p8,"Error in submitting data!");
                                    }
                                    JList li1=new JList(dl1);
                                    JList li2=new JList(dl2);
                                    JList li3=new JList(dl3);
                                    JList li9=new JList(dl9);
                                    li1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                                    li1.setVisibleRowCount(2);
                                    li2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                                    li2.setVisibleRowCount(2);
                                    li3.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                                    li3.setVisibleRowCount(2);
                                    li9.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                                    li9.setVisibleRowCount(2);
                                    f8.add(p8);
                                    g8.gridx=0;
                                    g8.gridy=0;
                                    p8.add(l40,g8);
                                    g8.gridx=1;
                                    g8.gridy=0;
                                    p8.add(t38,g8);
                                    g8.gridx=0;
                                    g8.gridy=1;
                                    p8.add(l41,g8);
                                    g8.gridx=1;
                                    g8.gridy=1;
                                    p8.add(new JScrollPane(li3),g8);
                                    g8.gridx=0;
                                    g8.gridy=2;
                                    p8.add(l42,g8);
                                    g8.gridx=1;
                                    g8.gridy=2;
                                    p8.add(new JScrollPane(li2),g8);
                                    g8.gridx=0;
                                    g8.gridy=3;
                                    p8.add(l43,g8);
                                    g8.gridx=1;
                                    g8.gridy=3;
                                    p8.add(new JScrollPane(li1),g8);
                                    g8.gridx=0;
                                    g8.gridy=4;
                                    p8.add(l56,g8);
                                    g8.gridx=1;
                                    g8.gridy=4;
                                    p8.add(new JScrollPane(li9),g8);
                                    g8.gridx=0;
                                    g8.gridy=5;
                                    p8.add(l44,g8);
                                    g8.gridx=1;
                                    g8.gridy=5;
                                    p8.add(t39,g8);
                                    g8.gridx=2;
                                    g8.gridy=5;
                                    p8.add(l45,g8);
                                    g8.gridx=0;
                                    g8.gridy=6;
                                    p8.add(b21,g8);
                                    g8.gridx=1;
                                    g8.gridy=6;
                                    p8.add(b22,g8);

                                    int a1=li1.getSelectedIndex();
                                    int a2=li2.getSelectedIndex();
                                    int a3=li3.getSelectedIndex();
                                    int a10=li9.getSelectedIndex();
                                    final String st1=(String) dl1.elementAt(a1);
                                    final String st2=(String) dl2.elementAt(a2);
                                    final String st3=(String) dl3.elementAt(a3);
                                    final String st10=(String)dl9.elementAt(a10);
4

1 に答える 1

0

では、BFPL.java の 851 行目は何ですか? それはあなたの問題だ。Vector#elementAt(n) を呼び出す前に、ベクターのサイズを確認してください。

于 2013-01-07T19:57:08.353 に答える