Como faço para ler de uma lista das classes abaixo. Ou seja tenho que ler TaxTableEntr a partir de TaxTable?
public class TaxTable {
protected List<TaxTableEntr> taxTableEntr;
public List<TaxTableEntr> getTaxTableEntr() {
if (taxTableEntr == null) {
taxTableEntr = new ArrayList<TaxTableEnt>();
}
return this.taxTableEntr;
}
public class TaxTableEntr {
protected String taxType;
protected String taxCountryRegion;
protected BigDecimal taxAmount;
// definição dos gets and sets
public String getTaxType() {
return taxType;
}
}
Fiz conforme indicado abaixo, mas me ocorre um erro: Exception in thread “main” java.lang.NullPointerException
ConstroiTaxTable taxTables = new ConstroiTaxTable(“D:\fileTax.TXT”);
TaxTable taxTable = new TaxTable();
masterFile.setTaxTable(taxTable);
masterFile.getTaxTable().getTaxTableEntr().addAll((Collection) taxTables);
- Use as tags [code]
- Ponha a declaração de ConstroiTaxTable também.
Cuidado: casts não são coisas mágicas.
Por exemplo, eles não servem para converter alguma coisa que você tem em alguma coisa que você quer “magicamente”.
Segue a classe ConstroiTaxTable
[import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.saft.classes.TaxTable;
import com.saft.classes.TaxTableEntr;
public class ConstroiTaxTable {
private String taxType;
private String taxCountryRegion;
private String taxAmount;
List<TaxTableEntr> taxTables = new ArrayList<TaxTableEntr>();
public List<TaxTableEntr> getTaxTableEntr() {
return taxTables;
}
public ConstroiTaxTable(String filePath) {
identificaOsCamposDoTxt(filePath);
}
private void mapeiaOobjecto() {
TaxTableEntry taxTableEntr = new TaxTableEntr();
taxTableEntry.setTaxType(taxType);
taxTableEntry.setTaxCountryRegion(taxCountryRegion);
BigDecimal vlor2 = new BigDecimal(taxAmount);
taxTableEntry.setTaxAmount(vlor2.divide(new BigDecimal("100")));
TaxTable taxTable = new TaxTable();
taxTable.getTaxTableEntr();
taxTables.add(taxTableEntr);
}
private void verCampos() {
System.out.println("taxType: "+taxType);
System.out.println("taxCountryRegion: "+taxCountryRegion);
System.out.println("taxAmount: "+taxAmount);
}
private void identificaOsCamposDoTxt(String filePath) {
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
while (( line = br.readLine())!= null){
taxType = line.substring(0, 3);
taxCountryRegion = line.substring(3, 8);
taxAmount = line.substring(8, 18);
//verCampos();
mapeiaOobjecto();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
]