问题
I am using the "ColumnPositionMappingStrategy" class of the opencvs
library.
This class has the deprecated
method setType(Class<T> type)
and as comment has "This method is deprecated as the user should use the Java 5 conventions"
As far as I understand is that I have to use Generics instead of setType like:
ColumnPositionMappingStrategy<MyClass> mappingStrategy = new ColumnPositionMappingStrategy<>();
to solve the problem. But I get NullPointerException
when I remove the line:
mappingStrategy.setType(MyClass.class);
error:
java.lang.RuntimeException: Error parsing CSV!
at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:95)
at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:75)
at csv_import.ReadCsvFile.readRecordData(ReadCsvFile.java:40)
at app.Application.main(Application.java:30)
Caused by: java.lang.NullPointerException
at com.opencsv.bean.HeaderColumnNameMappingStrategy.createBean(HeaderColumnNameMappingStrategy.java:170)
at com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:117)
at com.opencsv.bean.CsvToBean.processLine(CsvToBean.java:101)
at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:91)
... 3 more
What do I have to do to solve this problem?
回答1:
The implementation relies on the type by calling:
public T createBean() throws InstantiationException, IllegalAccessException {
return type.newInstance();
}
Some comments like this one - TODO refactor this class to use T instead of getType.
- imply that the @Deprecation
annotations have been used too early - as there is no alternative usage available.
来源:https://stackoverflow.com/questions/29455470/what-does-this-mean-this-method-is-deprecated-as-the-user-should-use-the-java-5