首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Supercsv找不到方法异常

Supercsv找不到方法异常
EN

Stack Overflow用户
提问于 2015-10-09 05:24:22
回答 1查看 1.4K关注 0票数 0

我有下面的实现。

代码语言:javascript
复制
csvReader = new CsvBeanReader(new InputStreamReader(stream), CsvPreference.STANDARD_PREFERENCE);
lastReadIdentity =  (T) csvReader.read(Packages.class, Packages.COLS);

在我的Packages.class

我已经设置了我的unitcount变量。

代码语言:javascript
复制
public String getUnitCount() {
    return unitCount;
}
public void setUnitCount(String unitCount) {
    this.unitCount = unitCount;
}

当它作为一个字符串时,它工作得很好,但是当它作为一个整数时,它会抛出下面的异常。请帮帮忙

代码语言:javascript
复制
private int unitCount;
public int getUnitCount() {
    return unitCount;
}
public void setUnitCount(int unitCount) {
    this.unitCount = unitCount;
}

例外:

代码语言:javascript
复制
org.supercsv.exception.SuperCsvReflectionException: unable to find method setUnitCount(java.lang.String) in class com.directv.sms.data.SubscriberPackages - 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:139)
at org.supercsv.util.MethodCache.getSetMethod(MethodCache.java:95)
EN

回答 1

Stack Overflow用户

发布于 2015-10-10 17:36:18

我对SuperCsv不太确定,但univocity-parsers应该能够毫无障碍地处理这个问题,更不用说它解析输入的速度至少快3倍。

只需注释您的类:

代码语言:javascript
复制
public class SubscriberPackages {

    @Parsed(defaultNullRead = "0") // if the file contains nulls, then they will be converted to 0.
    private int unitCount;   // The attribute name will be matched against the column header in the file automatically.
}

要将CSV解析为beans:

代码语言:javascript
复制
// BeanListProcessor converts each parsed row to an instance of a given class, then stores each instance into a list.
BeanListProcessor<SubscriberPackages> rowProcessor = new BeanListProcessor<SubscriberPackages>(SubscriberPackages.class);

CsvParserSettings parserSettings = new CsvParserSettings(); //many options here, check the tutorial.
parserSettings.setRowProcessor(rowProcessor); //uses the bean processor to handle your input rows
parserSettings.setHeaderExtractionEnabled(true); // extracts header names from the input file.

CsvParser parser = new CsvParser(parserSettings); //creates a parser with your settings.
parser.parse(new FileReader(new File("/path/to/file.csv"))); //all rows parsed here go straight to the bean processor

// The BeanListProcessor provides a list of objects extracted from the input.
List<SubscriberPackages> beans = rowProcessor.getBeans();

披露:我是这个库的作者。它是开源且免费的(ApacheV2.0许可证)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33026193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档