Error setting model in Jtable

Asked

Viewed 157 times

-1

So people, I was developing a frame that received a table from the database, I used the same scope of the function several times and it worked perfectly, I just modified the necessary to generate the table the way I wanted in this case and started to give error, I’ve been through the code searching and so far nothing...

public JTable preencheTabela(JTable tabela){

    ArrayList dados = new ArrayList();
    String[] Colunas = new String[]{"ID", "Cliente", "Valor", "Data"};
    int i = 1;

        do{
            dados.add(new Object[]{"aaaa"+i, 
                "aaa"+i, "aaa"+i, 
                "aasa"+i});
            i++;
        }while(i<5);

    ModeloTabela modelo = new ModeloTabela(dados, Colunas);
    tabela.setModel(modelo);//Erro acontece nessa linha      
    for(i = 0; i<Colunas.length; i++){
        tabela.getColumnModel().getColumn(i).setPreferredWidth(150);
        tabela.getColumnModel().getColumn(i).setResizable(false);
    }
    tabela.getTableHeader().setReorderingAllowed(false);
    tabela.setAutoResizeMode(tabela.AUTO_RESIZE_OFF);
    tabela.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    return tabela;

}

Model Table:

public class ModeloTabela extends AbstractTableModel{
private ArrayList linhas = null;
private String[] colunas = null;
public ModeloTabela(ArrayList lin, String[] col){
    this.setLinhas(lin);
    this.setColunas(col);
}
public ArrayList getLinhas(){
    return linhas;
}
public void setLinhas(ArrayList dados){
    linhas = dados;
}
public String[] getColunas(){
    return colunas;
}
public void setColunas(String[] nomes){
    colunas = nomes;
}
@Override
public int getColumnCount(){
    return colunas.length;
}
@Override
public int getRowCount(){
    return linhas.size();
}
@Override
public String getColumnName(int numCol){
    return colunas[numCol];
}
@Override
public Object getValueAt(int numLin, int numCol){
    Object[] linhas = (Object[])getLinhas().get(numLin);
    return linhas[numCol];
}

}

Place where I invoke the function:

public FrmCancelaVenda() {
    jTable = ctrl.preencheTabela(jTable);
    System.out.print("teste ");
    initComponents();
}

Error:

Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception At control.ControledCancelaVenda.fillTable(Controlecancelavenda.java:43) At Grafico.FrmCancelaVenda. (Frmcancelavenda.java:26)

1 answer

1

What happened was that I was trying to modify a jTable that had not yet been created by initComponents(), that is, just changing the call order of thePreencheTabela the problem has been solved.

Therefore, to solve the problem, it would be necessary to modify the constructor so that it would be like this:

public FrmCancelaVenda() {
    conecta.conexao();
    initComponents();
    jTable = ctrl.preencheTabela(jTable);
}
  • @diegofm in case answer in the question itself ?

  • Was it answer? Then ignore :p

  • It’s just that I found the mistake, I spent two days looking for, stupid on my part...

  • @diegofm in the case of programs that have multiple files, and in my case, a database, as I could post a minimum example is complete code?

  • Swing doubts, because it is about screens, are very difficult to analyze only with a snippet of code, and as you can see in the link, demonstrate the error through an isolated snippet of your code helps in solving the problem, and increase the chance of your question getting a quicker answer.

Browser other questions tagged

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