【发布时间】:2021-06-07 06:56:43
【问题描述】:
有问题的对象:
public class DataSupplyPoint {
private String date;
private double value;
private SupplyPoint supplyPoint;
public DataSupplyPoint(String date, double value, SupplyPoint supplyPoint) {
super();
this.date = date;
this.value = value;
this.supplyPoint = supplyPoint;
}
我的 csv 文件示例:
supplyPoint,date,value
0123456678,2021-03-10,2412.1999
0123456678,2021-06-10,3412.1999
0123456678,2021-09-10,4412.1999
0123456678,2021-12-10,5412.1999
0123456678,2021-03-10,2412.1999
0123456678,2021-06-10,3412.1999
0123456678,2021-09-10,4412.1999
0123456678,2021-12-10,5412.1999
我想做什么:
public ArrayList<DataSupplyPoint> importDataSupplyPointInCSV(String fileName) {
CsvMapper csvMapper = new CsvMapper();
CsvSchema schema = CsvSchema.emptySchema().withHeader();
ObjectReader oReader = csvMapper.readerFor(DataSupplyPoint.class).with(schema);
ArrayList<DataSupplyPoint> dataSupplyPointList = new ArrayList<>();
try(Reader reader = new FileReader(fileName + ".csv")) {
MappingIterator<DataSupplyPoint> mi = oReader.readValues(reader);
while (mi.hasNext()) {
DataSupplyPoint current = mi.next();
dataSupplyPointList.add(current);
}
return dataSupplyPointList;
} catch (IOException e) {
e.printStackTrace();
return dataSupplyPointList;
}
}
这让我犯了这个错误:
原因:com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造
user.SupplyPoint的实例(尽管至少存在一个Creator):没有字符串参数构造函数/工厂方法可以从字符串值反序列化('012345678912345678 ')'''
这是我的第一篇文章,我保证会改进啊哈
【问题讨论】:
-
您是否尝试过在网络上搜索“com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造实例”?我相信如果你把它放到你喜欢的搜索引擎中,你会发现一些关于你做错了什么的线索。如果您搜索堆栈溢出,您甚至可能会找到解决方案:)
-
欢迎来到这个网站。这里显示代码的首选方式是使用文本,而不是图像。这使人们可以更轻松地复制和粘贴它并重现您的问题。
-
是的,我有@Gavin。如果我最终决定发帖,那是因为我没有找到解决方案,而我事先寻找了解决方案:/我现在感觉很糟糕
-
谢谢@ThomasFritsch :)