Basic error handling - no row selected when deleting a row

I've been trying a few things, and was wondering if anybody had any advice or knew some websites that has some information on error handling. I need to refresh my memory a little. I have been attempting to make it work by using if-else statements, but I will probably need to use try-catch ones.

Thanks

This is the code for my remove button currently:

//REMOVE ROW BUTTON

JButton button2 =new JButton("Remove Row" );

buttonPanel.add( button2 );

button2.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

//System.out.println("You'll never get away this time.");

System.out.println("Remove Row Button Pressed.");

int[] selected =null;

//1.Error check in case no row is selected

//2.Ask the user if they really want to remove the row

selected = table.getSelectedRows();

System.out.println("selected: " + selected[0]);

//0 = 1st row (selected), 1 = 2nd row (one below selected one). Make the number equal to the selected row

sorter.removeRow(selected[0]);

}

});//Remove Row Button

This is the error message I get:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0

The code can handle it ok, and removes a row when it is selected. But I would like to be able to have a dialog box pop up and say "Please select a row to remove." And, I think it is a good programming habit to catch any errors that you can.

[2075 byte] By [Caps18a] at [2007-10-2 17:31:15]
# 1

I guess it works if I don't use an integer array. I don't need to do that anyway. I only want to be able to remove one row at a time.

//REMOVE ROW BUTTON

JButton button2 = new JButton( "Remove Row" );

buttonPanel.add( button2 );

button2.addActionListener( new ActionListener(){

public void actionPerformed(ActionEvent e)

{

System.out.println("Remove Row Button Pressed.");

int/*[]*/ selected = 0;

//1.Error check in case no row is selected

//2.Ask the user if they really want to remove the row

selected = table.getSelectedRow();

if (selected == -1) {

//Do nothing to prevent error

}

else{

System.out.println("selected: " + selected/*[0]*/);

//0 = 1st row (selected), 1 = 2nd row (one below selected one). Make the number equal to the selected row

sorter.removeRow(selected/*[0]*/);

}

}

});

Caps18a at 2007-7-13 18:48:12 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...