Computer Science Canada

There is an error when i run my program.ie;"java. lang.ArrayIndexOutOfBoundsException: -1"

Author:  afra_adi@yahoo.com [ Thu Aug 16, 2012 12:50 pm ]
Post subject:  There is an error when i run my program.ie;"java. lang.ArrayIndexOutOfBoundsException: -1"

im making a shopping program in java..and when ithe cart window opens,i wrote the following code so that when i select an item from my cart,i want its details in certain labels ect..n this " java.lang.ArrayIndexOutOfBoundsException: -1" error is occurring ..what is my mistake??help me to solve this

Java:
private void cartfWindowOpened(java.awt.event.WindowEvent evt) {                                   
   
    int n=carttbl.getSelectedRow();
    String value=(String)carttbl.getValueAt(n,0);
    try{
        stmt=con.createStatement();
        String q="select * from item where itemid='"+value+"';";
        ResultSet rs=stmt.executeQuery(q);
        while(rs.next()){
            cartpic.setIcon(new ImageIcon("C:\\Afra java\\pics\\"+rs.getString("pics")));
            stylelbl.setText(rs.getString("style"));
            colourlbl.setText(rs.getString("colour"));
            sizelbl.setText(rs.getString("size"));
            int qoh=rs.getInt("qoh");
            for(int i=1;i<=qoh;i++){
                qohcb.addItem(i);
            }
           
        }
    }
     catch(Exception e){
    JOptionPane.showMessageDialog(null,"Error occured"+e);
    }
}


Edit:Please wrap you code in:
code:
[syntax="java"] ... code ...[/syntax]

Author:  Insectoid [ Thu Aug 16, 2012 1:42 pm ]
Post subject:  RE:There is an error when i run my program.ie;"java. lang.ArrayIndexOutOfBoundsException: -1"

Quote:
java.lang.ArrayIndexOutOfBoundsException: -1


Read the error. It says "array index out of bounds: -1. Somewhere, you're passing -1 to an array, which is a very bad thing to do. Try to figure out where you do that, and you'll find your mistake.

Author:  md [ Thu Aug 16, 2012 11:13 pm ]
Post subject:  RE:There is an error when i run my program.ie;"java. lang.ArrayIndexOutOfBoundsException: -1"

The exception almost certainly tells you the line number as well, making it super easy to find out where the issue is.

I suspect it's near the top where you're trying to figure out which item is selected and aren't doing any error checking make sure that something is actually selected (aka getSelectedRow() returns -1 when nothing is selected).

Author:  afra_adi@yahoo.com [ Fri Aug 17, 2012 7:41 am ]
Post subject:  Re: RE:There is an error when i run my program.ie;"java. lang.ArrayIndexOutOfBoundsException: -1"

md @ Fri 17 Aug, 2012 08:13 wrote:
The exception almost certainly tells you the line number as well, making it super easy to find out where the issue is.

I suspect it's near the top where you're trying to figure out which item is selected and aren't doing any error checking make sure that something is actually selected (aka getSelectedRow() returns -1 when nothing is selected).


Hey..thanks a lot..i could figure out my mistake..thanks a lott..


: