0

可能重复:
异常处理“AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException:-1

我有以下错误:

线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException:-1
    在 java.util.Vector.elementData(Vector.java:730)
    在 java.util.Vector.elementAt(Vector.java:473)
    在 javax.swing.DefaultListModel.elementAt(DefaultListModel.java:266)
    在 bfpl.BFPL$1$2$17.actionPerformed(BFPL.java:1163)
    在 javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    在 javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    在 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)

我的代码是:

    final JFrame f10=new JFrame("Match Result");
    f10.setVisible(true);
    f3.setVisible(false);
    f10.setSize(1000,1000);
    f10.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JPanel p10=new JPanel(new GridBagLayout());
    GridBagConstraints g10=new GridBagConstraints();
    g10.insets=new Insets(2,2,2,2);
    JLabel l53=new JLabel("Match No");
    JLabel l54=new JLabel("Team Win");
    JLabel l55=new JLabel("Team Lost");
    DefaultListModel dl6=new DefaultListModel();
    DefaultListModel dl7=new DefaultListModel();
    DefaultListModel dl8=new DefaultListModel();
    String f="SELECT Match_No,Team1,Team2 from Schedule";
    try{

        Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/BFPL","root","ilovepepsi");
        Statement s=con.createStatement();
        ResultSet rs=s.executeQuery(f);
        while(rs.next()){
            String z=rs.getString("Match_No");
            String z1=rs.getString("Team1");
            String z2=rs.getString("Team2");
            dl6.addElement(z);
            dl7.addElement(z1);
            dl8.addElement(z2);
        }
    }
    catch(SQLException ae){
        System.out.println("SQLException:"+ae.getMessage());
        System.out.println("SQLState:"+ae.getSQLState());
        System.out.println("VendorError:"+ae.getErrorCode());
        JOptionPane.showMessageDialog(p10,"Error in submitting data!");
    }
    JList li6=new JList(dl6);
    JList li7=new JList(dl7);
    JList li8=new JList(dl8);
    li6.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    li6.setVisibleRowCount(2);
    li7.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    li7.setVisibleRowCount(2);
    li8.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    li8.setVisibleRowCount(2);
    JButton b25=new JButton("Back");
    JButton b26=new JButton("Save");
    f10.add(p10);
    g10.gridx=0;
    g10.gridy=0;
    p10.add(l53,g10);
    g10.gridx=1;
    g10.gridy=0;
    p10.add(new JScrollPane(li6),g10);
    g10.gridx=0;
    g10.gridy=1;
    p10.add(l54,g10);
    g10.gridx=1;
    g10.gridy=1;
    p10.add(new JScrollPane(li7),g10);
    g10.gridx=0;
    g10.gridy=2;
    p10.add(l55,g10);
    g10.gridx=1;
    g10.gridy=2;
    p10.add(new JScrollPane(li8),g10);
    g10.gridx=0;
    g10.gridy=3;
    p10.add(b25,g10);
    g10.gridx=1;
    g10.gridy=3;
    p10.add(b26,g10);
    int a9=li6.getSelectedIndex();
    int a10=li7.getSelectedIndex();
    int a11=li8.getSelectedIndex();
    final String z=(String) dl6.elementAt(a9);
    final String z1=(String) dl7.elementAt(a10);
    final String z2=(String)dl8.elementAt(a11);
4

1 回答 1

2

您将 getSelectedIndex() 的结果传递给 elementAt()。如果在列表框中未选择任何内容,则该值将超出范围。

于 2013-01-07T18:45:22.327 回答