-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)
@diegofm in case answer in the question itself ?
– Vitor Gabriel Andrade
Was it answer? Then ignore :p
– user28595
It’s just that I found the mistake, I spent two days looking for, stupid on my part...
– Vitor Gabriel Andrade
@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?
– Vitor Gabriel Andrade
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.
– user28595