2

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?

4
  • Check if templates.getClass() returns null Commented Apr 14, 2015 at 14:30
  • it returns (java.lang.Class<T>) class java.util.ArrayList. But componentType returns null. So it seems cannot recognize component type Commented Apr 14, 2015 at 14:31
  • templates.getClass().getComponentType() should probably be templates.getClass() - also note that your toArray method is fairly easy to test so you should probably be able to isolate the problem with a unit test and fix it. Commented Apr 14, 2015 at 14:33
  • I used display tab in Eclipse to recognize problem. Commented Apr 14, 2015 at 14:35

1 Answer 1

3

If you see the javadoc for getComponentType() it says...

Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.

Sign up to request clarification or add additional context in comments.

1 Comment

I see. Well I decided to add additional method to command class called getClassOfK which allows me to get K class and instantiate it. But I am not sure about this solution. It works now though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.