Error when setting template in JTable

-1
So 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 what was necessary to generate the table the way I wanted it in that case and started give error, I've already searched the code 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;

}

Table Template:

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];
}

}

Location 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.ControleCancelaVenda.preencheTabela (ControlCancelaVenda.java:43)       

asked by anonymous 06.12.2016 / 20:34

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 PreencheTabela the problem was solved.

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

public FrmCancelaVenda() {
    conecta.conexao();
    initComponents();
    jTable = ctrl.preencheTabela(jTable);
}
    
07.12.2016 / 18:24