Error accessing Jtable line

Asked

Viewed 94 times

-1

In my system is giving error in the line of code

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 am trying to throw the dice from the table on a Jtextfield, but when I click on the button appears this errorinserir a descrição da imagem aqui

  • What is this line variable? What is its origin? Add to the question as well.

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

 //recebe a linha selecionada
 int linha = this.jTableUsuarios.getSelectedRow();

  • Please add a [mcve] of your code, so it is possible to reproduce the problem.

1 answer

1

Looking through your image would say that you are trying to get the index of the selected line when there is no selected line, see in documentation:

public int getSelectedRow()  

Returns the index of the first Selected Row, -1 if no Row is Selected.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.