Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Apache Commons CSVParser.getHeaderMap()总是返回null?_Java_Csv_Apache Commons_Apache Commons Csv - Fatal编程技术网

Java 为什么Apache Commons CSVParser.getHeaderMap()总是返回null?

Java 为什么Apache Commons CSVParser.getHeaderMap()总是返回null?,java,csv,apache-commons,apache-commons-csv,Java,Csv,Apache Commons,Apache Commons Csv,使用Apache Commons CSV读取以下TSV代码段时: Name DOB SIN Address, contact information "Patience Middleton" "18-4-87" 720463771 "varius Cras sem aliquam taciti fames hendrerit tempor" 这是我的代码: CSVFormat format = CSVFormat.newFormat('\t').withQuote('"')

使用Apache Commons CSV读取以下TSV代码段时:

Name    DOB SIN Address, contact information
"Patience Middleton"    "18-4-87"   720463771   "varius Cras sem aliquam taciti fames hendrerit tempor"
这是我的代码:

CSVFormat format = CSVFormat.newFormat('\t').withQuote('"');
CSVParser parsed = CSVParser.parse(csvData, format);
List<CSVRecord> record = parsed.getRecords();
System.out.println(parsed.getHeaderMap().toString());
CSVFormat format=CSVFormat.newFormat('\t')。带引号('“');
CSVParser parsed=CSVParser.parse(csvData,格式);
List record=parsed.getRecords();
System.out.println(parsed.getHeaderMap().toString());
然而,我总是得到一个
NullPointerException
声明
parsed.getHeaderMap()==null

根据API(),该方法可能返回按列顺序迭代的头映射的副本


我的代码或CSV文件中是否有错误?库是否失败?

默认情况下
CSVParser
假定没有标题行,并将第一行解析为常规数据。您需要明确告诉它,您希望通过调用
CSVFormat
将CSV文件的第一行解释为标题:

CSVParser parsed = CSVParser.parse(csvData, 
   CSVFormat.newFormat('\t').withQuote('"').withHeader());

非常感谢!我在withHeader()函数中看到了may参数,天真地认为它们只用于手动设置标题。