What does this mean “This method is deprecated as the user should use the Java 5 conventions.”?

守給你的承諾、 提交于 2019-12-25 03:15:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!