Pegar varios elementos de uma Jtable[resolvido]

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

isso msm +) dai tem q da um new HashSet<Item>();
?? e vai funcionar? a logica ta certa?

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

[code]class TableModel extends AbstractTableModel{
private String[] colunas = {“Codigo”,“Nome”,“Descrição”,“valor”};

	ArrayList<Item> colecao = new ArrayList<Item>();
	ItemDao dao = ItemDao.getInstance();
	
	public TableModel(){
		colecao = dao.obterItems();
	}
	
	@Override
	public int getColumnCount() {
		return colunas.length;
	}

	@Override
	public int getRowCount() {
		return colecao.size();
	}
	
	@Override
	public String getColumnName(int column) {
		return colunas[column];
	}
	
	@Override
	public Object getValueAt(int row, int column) {
		Item i = colecao.get(row);
		if(column ==0){
			return i.getCod();
		}
		if(column ==1){
			return i.getNome();
			
		}
		if(column ==2){
			return i.getDescricao();
		}
		else{
			return i.getValor();
		}
		
	}
	public void listar(){
		colecao = dao.obterItems();
		fireTableDataChanged();
	}
	
	public Item getItem(int index){
		Item item = colecao.get(index);
		return item;
	}
	
}[/code]

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() : new ArrayList<Receber>();
    for (int i = 0; i < values.size(); i++) {              
            Receber receber = (Receber) values.get(i);
}

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.

[code]import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class TableModel extends AbstractTableModel {

private static final long serialVersionUID = -8529810359520768805L;

private String[] columns;

private List<RowTable> rows;

public TableModel(List<RowTable> rows, String[] columns) {
	this.setRows(rows);
	this.setColumns(columns);
}	

public void setRows(List<RowTable> rows) {
	if(getRows() == null && rows == null)
		return;
	fireTableRowsDeleted(0, (getRowCount() != 0 ? getRowCount() : 1) - 1);
	this.rows = rows;
	if(getRows() != null)
		fireTableRowsInserted(0, rows.size() - 1);
}

public void addRow(RowTable rowTable) {
	if(getRows() == null)
		setRows(new ArrayList<RowTable>());
	int row = getRowCount();
	getRows().add(rowTable);
	fireTableRowsInserted(row, row);
}

public void removeRow(int rowIndex) {
	getRows().remove(rowIndex);
	fireTableRowsDeleted(rowIndex, rowIndex);
}

public void editRow(int rowIndex, RowTable rowTable) {
	getRows().set(rowIndex, rowTable);
	fireTableRowsUpdated(rowIndex, rowIndex);
}

public RowTable getRow(int rowIndex) {
	return getRows().get(rowIndex);
}

public List<RowTable> getRows() {
	return rows;
}

public void setColumns(String[] columns) {
	this.columns = columns;
}

public String[] getColumns() {
	return columns;
}

@Override
public String getColumnName(int column) {
	return getColumns()[column];
}

@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
	getRows().get(rowIndex).getRowData()[columnIndex] = (String) value;
	fireTableCellUpdated(rowIndex, columnIndex);
}	

@Override
public int getColumnCount() {
	return getColumns().length;
}

@Override
public int getRowCount() {
	return getRows() != null ? getRows().size() : 0;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
	return getRows() != null ? getRows().get(rowIndex).getColRowData(columnIndex) : null;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
	return true;
}[/code]

[quote=lgweb]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() : new ArrayList<Receber>();
    for (int i = 0; i < values.size(); i++) {              
            Receber receber = (Receber) values.get(i);
}

[/quote]

quero so as selecionadas msm brigadao ^^

mas eu quero o nome das colunas fixo msm