我正在尝试使用Apache Commons CSV将包含指定标题的CSV文件读入Java对象。但是,当我运行代码时,我会收到以下异常:
Exception in thread "main" java.lang.IllegalArgumentException: Mapping for Color not found, expected one of [Color, Name, Price, House Cost, Rent, 1 House, 2 Houses, 3 Houses, 4 Houses, Hotel, Mortgage]
at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:102)
at GameBoard.<init>(GameBoard.java:25)
at Game.main(Game.java:3)
有人能解释一下这个异常是从哪里来的吗?在我看来,Apache Commons 似乎无法将我的输入与某个列匹配。是我的某个部分有问题还是其他地方出了错?以下是我的代码片段:
Reader in;
Iterable<CSVRecord> records = null;
try {
in = new FileReader(new File(Objects.requireNonNull(getClass().getClassLoader().getResource("Properties.csv")).getFile()));
records = CSVFormat.EXCEL.withFirstRecordAsHeader().parse(in);
} catch (IOException | NullPointerException e) {
e.printStackTrace();
System.exit(1);
}
for (CSVRecord record :
records) {
spaces.add(new Property(
record.get("Color"),
record.get("Name"),
Integer.parseInt(record.get("Price")),
以下是我的CSV文件头(抱歉,其中一个被截断了,但这不是重点):
谢谢!