首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带opencsv CsvToBean的空指针过量

带opencsv CsvToBean的空指针过量
EN

Stack Overflow用户
提问于 2015-09-11 11:20:30
回答 1查看 2.5K关注 0票数 3

我正试图使用openCSV.Each行将CSV文件中的所有行转换为java,我的文件中有21列与管道符号(X).But分隔,.But获得空指针异常,而csv文件中的code.rows包含空单元格。

代码语言:javascript
复制
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;
        }

    }

下面是错误堆栈跟踪:

线程“主”java.lang.RuntimeException中的异常:解析CSV错误!在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) at : 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) .

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-14 06:00:57

我想你在单级解析器上的麻烦会少一些。它也比OpenCSV快得多。要使用它,首先注释您的bean:

代码语言:javascript
复制
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;
    ...

}

解析:

代码语言:javascript
复制
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();

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

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

https://stackoverflow.com/questions/32522227

复制
相关文章

相似问题

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