I've got such method
@SuppressWarnings("unchecked")
public static <T, K> K[] toArray(ITemplateCommand<T,K> command, List<T> templates) {
if (null == templates) {
return null;
}
K[] array = (K[]) Array.newInstance(templates.getClass().getComponentType(), templates.size());
for (int i = 0; i < templates.size(); i++) {
array[i] = command.buildTemplate(templates.get(i));
}
return array;
}
And I call it there
@Override
public TemplateResponse buildTemplateResponse(List<NotificationTemplateDetails> templates, String offset,
String perPage, String sort, String sortOrder, int total) {
TemplateResponse templateResponse = new TemplateResponse();
templateResponse.setItems(ResponseUtils.<NotificationTemplateDetails, Template>toArray(new ToTemplateCommand(), templates));
templateResponse.setTotal(total);
templateResponse.setRequest(buildRequestInformation(offset, perPage, sort, sortOrder));
return templateResponse;
}
Where ToTemplateCommand is an implementation of ITemplateCommand iterface
public class ToTemplateCommand implements ITemplateCommand<NotificationTemplateDetails, Template> {
@Override
public Template buildTemplate(NotificationTemplateDetails template) {
....
But I've NullPointerException on
K[] array = (K[]) Array.newInstance(templates.getClass().getComponentType(), templates.size());
line.
java.lang.NullPointerException: null
at java.lang.reflect.Array.newArray(Native Method) ~[na:1.7.0_21]
at java.lang.reflect.Array.newInstance(Array.java:70) ~[na:1.7.0_21]
....
But neither command nor templates parameters are null.
What's the problem?
templates.getClass()returns null(java.lang.Class<T>) class java.util.ArrayList. ButcomponentTypereturns null. So it seems cannot recognize component typetemplates.getClass().getComponentType()should probably betemplates.getClass()- also note that yourtoArraymethod is fairly easy to test so you should probably be able to isolate the problem with a unit test and fix it.displaytab in Eclipse to recognize problem.