java - No header mapping was specified, the record values can't be accessed by name (Apache Commons CSV) -
i got error message happening when i'm trying read csv:
exception in thread "main" java.lang.illegalstateexception: no header mapping specified, record values can't accessed name @ org.apache.commons.csv.csvrecord.get(csvrecord.java:99) @ mockdata.mockdata.main(mockdata.java:33)
java result: 1
i'm using apache commons csv library 1.1. tried googling error message , thing code listing on sites grepcode.
here's code:
package mockdata; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstreamreader; import java.io.reader; import org.apache.commons.csv.csvformat; import org.apache.commons.csv.csvrecord; public class mockdata { /** * @param args command line arguments */ public static void main(string[] args) throws filenotfoundexception, ioexception { reader in = new inputstreamreader(mockdata.class.getclassloader() .getresourceasstream("mock_data.csv"), "utf-8"); iterable<csvrecord> records = csvformat.excel.parse(in); (csvrecord record : records) { string lastname = record.get("last_name"); string firstname = record.get("first_name"); system.out.println("firstname: " + firstname + " lastname: " + lastname); } } }
the contents of csv:
first_name,last_name,address1,city,state,zip,country,phone,email robin,lee,668 kinsman road,hagerstown,tx,94913,united states,5-(078)623-0713,rlee0@e-recht24.de bobby,moreno,68 dorton avenue,reno,az,79934,united states,5-(080)410-6743,bmoreno1@ihg.com eugene,alexander,3 bunker hill court,newark,ms,30066,united states,5-(822)147-6867,ealexander2@gmpg.org katherine,crawford,3557 caliangt avenue,new orleans,or,23289,united states,2-(686)178-7222,kcrawford3@symantec.com
it's located in src folder.
calling withheader() default excel csv format worked me:
csvformat.excel.withheader().parse(in);
the sample in documentation not clear, can found here : referencing columns safely: if source contains header record, can simplify code , safely reference columns, using withheader(string...) no arguments: csvformat.excel.withheader();
Comments
Post a Comment