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)