Error accessing JTable line [closed]

-1

On my system you are giving code line error

ModelUsuario modelUsuario = new ModelUsuario();
    ControllerUsuario controllerUsuario = new ControllerUsuario();

    //recebe a linha selecionada
    int linha = this.jTableUsuarios.getSelectedRow();
    int codigo = (Integer) jTableUsuarios.getValueAt(linha, 0); //erro aqui

I'm trying to throw the table data into a JTextField, but as I click the button this error appears

    
asked by anonymous 25.04.2017 / 03:24

1 answer

1

Looking at your image, I'd say you're trying to grab the index of the selected row when you have no row selected, see Documentation :

public int getSelectedRow()  
     

Returns the index of the first selected row, -1 if no row is selected.

When you try to get the selected line with getValueAt() and index -1 it returns you a IndexOutOfBoundsException: Invalid index .

To solve check before getValueAt() if the index is greater than or equal to 0, otherwise treat the situation, for example displaying a message warning that the user must select a line before clicking the button.

    
25.04.2017 / 14:03