-
Read CSV file
Hello,
I'm pretty new in Java, I try to read csv file into Data class. I use superCsv, but I have an error :(
Code:
Error while filling an object context: null offending processor: null
at org.supercsv.io.CsvBeanReader.fillObject(Unknown Source)
at org.supercsv.io.CsvBeanReader.read(Unknown Source)
at dataLoader.ReadFile.loadFromCSV(ReadFile.java:36)
at main.Program.main(PPR.java:16)
Caused by: java.lang.InstantiationException: data.Data
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
... 4 more
Code:
Locale.setDefault(Locale.US);
CsvPreference pref = new CsvPreference(';', ';', ";");
FileReader fr = new FileReader(csvfile);
ICsvBeanReader inFile = new CsvBeanReader(fr, pref);
try{
Data data;
String[] header = inFile.getCSVHeader(true);
while((data = inFile.read(Data.class, header, processors))!=null){
System.out.println(data.getTime());
System.out.println(data.getVal1());
}
}
finally{
inFile.close();
}
}
static CellProcessor[] processors = new CellProcessor[]{
new ParseDate("HH:mm:sss"),
new StrMinMax(1,30), //value 1,
null, // value 2
new StrMinMax(0, 20)// value 3
};
actually value 1,2,3 are double, but I have error when I try to use ParseDouble, may be because values in CSV file has the following format #,#####.
Thank you.
-
Re: Read CSV file
For a moment change the content and give a try with a dummy file. Then you can make sure it. Since the exception you ends with not provide much details, seems that is the best way.
-
Re: Read CSV file
Oh, I've solved the problem, I used EXCEL_NORTH_EUROPE_PREFERENCE and it worked, and one of the name of the columns was with capitals "VA", but I had setVa, while setVA is correct. Anyway, Thnx a lot. :)
-
Re: Read CSV file
You are welcome (even I have not really helping you to solve the problem)
However, thanks a lot for posting that the way you solved it. There may be other members in the forums that stuck with the same issue.