Eu estou com a seguinte dificuldade tenho uma table, e preciso pegar varios elementos da msm e adicionar a uma coleção eu fiz o seguinte
tentando pegar os varios itens da table
int indexes[] = table.getSelectedRows();
Collection<Item> itens = null;
TableModel model = (TableModel) table.getModel();
for (int i = 0; i < indexes.length; i++) {
Item item = model.getItem(i);
itens.add(item);
}
metodo getItem
public Item getItem(int index){
Item item = colecao.get(index);
return item;
erro
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at br.com.oficina.view.CadastroServico$1.mousePressed(CadastroServico.java:122)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
sua Collection está nula (Collection<Item> itens = null;), então quando você tenta fazer itens.add(item); dá NullPointerException
G
guialeixo
isso msm +) dai tem q da um new HashSet<Item>();
?? e vai funcionar? a logica ta certa?
Gerson_da_S_Lima
eu usaria um new ArrayList<Item>(), mas o HashSet acho que também funciona, e com relação a lógica acho que está OK, só uma pergunta, aquele TableModel foi você que implementou? Se não aconselho que você implemente.
G
guialeixo
Gerson da S. Lima:
eu usaria um new ArrayList<Item>(), mas o HashSet acho que também funciona, e com relação a lógica acho que está OK, só uma pergunta, aquele TableModel foi você que implementou? Se não aconselho que você implemente.
sim foi eu sim porem aproveitando eu nao estou conseguindo colocar o titulo dela se puder me ajudar
Cara selectedRows() so vai trazer as linhas selecionadas se vc quer pegar tds as linhas tente assim:
List<Receber>values=view.getTbItens().getValues()!=null?view.getTbItens().getValues():newArrayList<Receber>();for(inti=0; i < values.size(); i++) { Receberreceber=(Receber)values.get(i);
}
Gerson_da_S_Lima
Cara eu acho que o método que pega o nome das colunas está correto, mas tá meio ruim, o nome das colunas está fixo, tem esse código que eu fix, tá meio ruim ainda, vou ver se melhoro, mas vê se te ajuda.