gpt4 book ai didi

java - opencsv CsvToBean 的空指针异常

转载 作者:行者123 更新时间:2023-12-02 09:15:09 24 4
gpt4 key购买 nike

我正在尝试使用 openCSV 将 CSV 文件中的所有行转换为 java bean。文件中每行有 21 列,用管道符号 (|) 分隔。但是使用此代码得到空指针异常。csv 文件中的行还包括空单元格。我无法弄清楚错误在哪里。有人可以帮助我吗

package com.alu.mdf.testsuite.sure;
import java.io.FileReader;
import java.util.List;
import com.opencsv.CSVParser;
import com.opencsv.CSVReader;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.bean.CsvToBean;

//import com.alu.mdf.test.common.Person;

public class CSVExplorer {

@SuppressWarnings({"rawtypes", "unchecked"})
public static void main(String[] args) throws Exception
{
CsvToBean csv = new CsvToBean();

String csvFilename = "TestCaseConfigurationFiles/application.csv";
//CSVReader csvReader = new CSVReader;
CSVParser csvParser=new CSVParser('|');
CSVReader reader = new CSVReader(new FileReader(csvFilename),1,csvParser);



//Set column mapping strategy
List list = csv.parse(setColumMapping(), reader);

for (Object object : list) {
SUREDataBean SUREDataBean = (SUREDataBean) object;
System.out.println(SUREDataBean);
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
private static ColumnPositionMappingStrategy setColumMapping() throws Exception
{
ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
strategy.setType(SUREDataBean.class);
//strategy.createBean();
String[] columns = new String[] {"InputDataStartIdentifier","EntityType","Operation","IncludeId","IdValue","AssociatedResource","SearchQueryForGETRequest/ParametersForPUTRequest","PayloadLocation","TestCaseName","Description","userName","password","InputDataEndIdentifier","ValidationDataStart","ExpectedStatusCode","VerficationParameters","Method","class","Prerequisites","Group","ValidationDataEnd"};
System.out.println(columns.length);
strategy.setColumnMapping(columns);
return strategy;
}

}

这是错误堆栈跟踪:

Exception in thread "main" java.lang.RuntimeException: Error parsing CSV! at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:95) at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:75) at com.alu.mdf.testsuite.sure.CSVExplorer.main(CSVExplorer.java:28) Caused by: java.lang.NullPointerException at com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:123) at com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:101) at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:91) ...

最佳答案

我想你会遇到 uniVocity-parsers 的麻烦更少。它也比 OpenCSV 快得多。要使用它,首先注释您的 bean:

class SUREDataBean {

@NullString(nulls = { "?", "-" }) // if the value parsed in the quantity column is "?" or "-", it will be replaced by null.
@Parsed(defaultNullRead = "0") // if a value resolves to null, it will be converted to the String "0".
private Integer quantity; // The attribute name will be matched against the column header in the file automatically.

@Trim
@LowerCase
@Parsed
private String comments;
...

}

解析:

BeanListProcessor<SUREDataBean> rowProcessor = new BeanListProcessor<SUREDataBean>(SUREDataBean.class);

CsvParserSettings parserSettings = new CsvParserSettings();
settings.getFormat().setDelimiter('|');
parserSettings.setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);

CsvParser parser = new CsvParser(parserSettings);

//Parsing is started here.
//this submits all rows parsed from the input to the BeanListProcessor
parser.parse(new FileReader(new File("/examples/bean_test.csv")));

List<SUREDataBean> beans = rowProcessor.getBeans();

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

关于java - opencsv CsvToBean 的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32522227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com