我正在尝试使用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;
}
}
这是错误堆栈跟踪:
线程“main”中的异常java.lang.RuntimeException:解析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)由com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:123)的com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:101)处的java.lang.NullPointerException引起。 opencsv.bean.CsvToBean.parse(CsvToBean.java:91)...