【问题标题】:How to properly solve this : Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1如何正确解决此问题:线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException: -1
【发布时间】:2018-04-23 15:34:33
【问题描述】:

我不确定如何正确设置它而不会出现

的错误

线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException: -1

int row=table_1.getSelectedRow();
int column=4;                                
// If I put it here I get the error and won't run at all the if below   
// String tableRoCol = table_1.getModel().getValueAt(row, column).toString();                                                   
if (row==-1 ) { 
    JOptionPane.showMessageDialog(null, "Please select a room to continue!");                               
}                               
// If I put it here I get the error after it run    
// String tableRoCol = table_1.getModel().getValueAt(row, column).toString();                               
String tableRoCol = table_1.getModel().getValueAt(row, column).toString();                          
if ( tableRoCol.equals("Booked")) {
    JOptionPane.showMessageDialog(null, "Please select a room that is Free to continue!");                              
} else {
    column =3;                                   
    String tableColRoom = table_1.getModel().getValueAt(row, column).toString();
    System.out.println("THE room ID IS :"+tableColRoom);                                
}                           

【问题讨论】:

标签: java if-statement


【解决方案1】:

不确定我是否理解正确,但我假设如果 row==-1,您不想执行其余代码,如果是这种情况,只需将其余代码包装在 else 块中。

int row=table_1.getSelectedRow();
int column=4;                                

if (row==-1 ) { 
    JOptionPane.showMessageDialog(null, "Please select a room to continue!");                               
}
else {
    String tableRoCol = table_1.getModel().getValueAt(row, column).toString();                          
    if ( tableRoCol.equals("Booked")) {
        JOptionPane.showMessageDialog(null, "Please select a room that is Free to continue!");                              
    } else {
        column =3;                                   
        String tableColRoom = table_1.getModel().getValueAt(row,       column).toString();
        System.out.println("THE room ID IS :"+tableColRoom);                                
    }  
}                         

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多