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)
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.
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.