my tsv file contains :
tax_id GeneID Symbol
9606 1 A1BG
TsvReader.java
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] {
new NotNull(), // tax_id
new NotNull(), // GeneID
new NotNull(), // Symbol
};
return processors;
}
private static void readCsvBeanReader() throws Exception {
ICsvBeanReader beanReader = null;
try {
beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME),
CsvPreference.TAB_PREFERENCE);
// the header elements are used to map the values to the bean (names
// must match)
final String[] header = beanReader.getHeader(true);
final CellProcessor[] processors = getProcessors();
TsvEntities tsv_ent;
while ((tsv_ent = beanReader.read(TsvEntities.class, header,
processors)) != null) {
System.out.println(String.format("tsv_ent=%s",tsv_ent));
}
} finally {
if (beanReader != null) {
beanReader.close();
}
}
}
TsvEntities.java
package com.tsvreader;
public class TsvEntities {
private Integer tax_id;
private Integer GeneID;
private String Symbol;
public TsvEntities() {
}
public TsvEntities(final Integer tax_id, final Integer GeneID, final String Symbol){
this.tax_id = tax_id;
this.GeneID= GeneID;
this.Symbol=Symbol;
}
public Integer getTax_id() {
return tax_id;
}
public void setTax_id(Integer tax_id) {
this.tax_id = tax_id;
}
public Integer getGeneID() {
return GeneID;
}
public void setGeneID(Integer geneID) {
this.GeneID = geneID;
}
public String getSymbol() {
return Symbol;
}
public void setSymbol(String symbol) {
this.Symbol = symbol;
}
@Override
public String toString() {
return String
.format("TsvEntities [tax_id=%s, GeneID=%s, Symbol=%s]",getTax_id(), getGeneID(), getSymbol());
}
EXception :
Exception in thread "main" org.supercsv.exception.SuperCsvReflectionException: unable to find method setTax_id GeneID Symbol(java.lang.String) in class com.tsvreader.TsvEntities - check that the corresponding nameMapping element matches the field name in the bean, and the cell processor returns a type compatible with the field
context=null
at org.supercsv.util.ReflectionUtils.findSetter(ReflectionUtils.java:183)
at org.supercsv.util.MethodCache.getSetMethod(MethodCache.java:95)
at org.supercsv.io.CsvBeanReader.populateBean(CsvBeanReader.java:158)
at org.supercsv.io.CsvBeanReader.read(CsvBeanReader.java:207)
at com.tsvreader.TsvReader.readCsvBeanReader(TsvReader.java:59)
at com.tsvreader.TsvReader.main(TsvReader.java:17)
From a first glance I would say that your problem is that public void setTax_id(Integer tax_id)
takes one Integer
parameter but SuperCsv expects it to take a String
. You should provide a setTax_id
method that takes a String
. The same should also be true for setGeneID
.
Edit:
The first part was incorrect. Seems like the header of your file is not parsed correctly. SuperCsv interprets the whole line as the method name. Check that the first line correctly separated. (From your question it seems that the first line is separated by spaces rather than tabs.)