简体   繁体   中英

Sightly : throwing null pointer exception for existing method

I am trying to create my own image handle. The class is using a user defined class MySmartImage.java which has com.day.cq.wcm.foundation.Image, and two string properties and their getter setter as follows.

public class MySmartImage extends WCMUse {

    private Image image;
    private String titleText;
    private String descText;

    @PostConstruct
    public void activate() {

    }

    public MySmartImage(Resource resource) {
        this.image = new Image(resource);

    }

    public MySmartImage(Image image, String titleText, String descText) {
        this.image = image;
        this.titleText = titleText;
        this.descText = descText;
    }

    public MySmartImage(Resource resource, String titleText, String descText) {
        this.image = new Image(resource);
        this.titleText = titleText;
        this.descText = descText;
    }

    public Image getImage() {

        return image;
    }

    public String getTitleText() {
        return "test";
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
    }

    public void setDescText(String descText) {


        this.descText = descText;
    }

    public String getDescText() {
        return descText;
    }

}

And my handler is as follows:

Hi i've implemented getImages as follows in the handler now.

public class ImageHandler extends WCMUse {

private String param;

private static Resource resource;

private Test test;

private List<MySmartImage> images = new ArrayList<MySmartImage>();

@Override
public void activate() throws Exception {
   resource = getResource();
    test = new Test();
    //images = this.getImages();
   //param = get("param", String.class);
}

public List<MySmartImage> getImages() {

    System.out.println("Resouce ::::" + resource);
    List<MySmartImage> images = new ArrayList<MySmartImage>();
    Resource imagesResource = resource.getChild("images");
    System.out.println("Images ::::" + images);

    if (imagesResource == null) {
        return images;
    }

    ValueMap map = imagesResource.adaptTo(ValueMap.class);
    String order = map.get("order", String.class);

    if (order == null) {
        return images;
    }

    System.out.println("Order :::: " + order);


    JSONArray array; ValueMap vMap;

    try {
        array = new JSONArray(order);
    } catch (JSONException e) {
        array = new JSONArray();
    }

    for (int i = 0; i < array.length(); i++) {
        String imageResourceName;

        try {
            imageResourceName = array.getString(i);
        } catch (JSONException e) {
            imageResourceName = null;
        }

        System.out.println("image resource name :::: " + imageResourceName);

        if (imageResourceName != null) {
            Resource imageResource = imagesResource.getChild(imageResourceName);

            System.out.println("imageResource :::: " + imageResource);

            if (imageResource != null) {
                Iterator childImagesItr = imageResource.listChildren();

                System.out.println("childImagesItr :::: " + childImagesItr);

                while(childImagesItr.hasNext()){
                    Resource childImage = (Resource)childImagesItr.next();
                    Image image = new Image(childImage);
                    image.setItemName(Image.PN_REFERENCE,"imageReference");
                    image.setSelector("img");
                    image.setAlt(childImage.getName());

                    vMap = imageResource.adaptTo(ValueMap.class);
                    String title = vMap.get("titleText", String.class);
                    String desc = vMap.get("descText", String.class);
                    MySmartImage img = new MySmartImage(childImage);
                    img.setTitleText(title);
                    img.setDescText(desc);
                    images.add(img);
                    System.out.println("image added in images :::: " + images.size());
                }
            }
        }
    }

    System.out.println("image added in images :::: " + images.getClass());
    return images;
}

}

So when on my sightly page I am trying to access the image handler as follows.

 <div data-sly-use.handle="ImageHandler"> <div data-sly-list.image="${handle.images }"> -----${image.titleText} </div> </div>

It is giving following exception

org.apache.sling.scripting.sightly.SightlyException: java.lang.NullPointerException at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.getObjectNoArgMethod(RenderContextImpl.java:350) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.getObjectProperty(RenderContextImpl.java:323) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.getProperty(RenderContextImpl.java:281) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.resolveProperty(RenderContextImpl.java:103) at apps.gbp.components.content.c_stage2.SightlyJava_c_stage2.render(SightlyJava_c_stage2.java:54) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderUnit.render(RenderUnit.java:52) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.evaluateScript(SightlyScriptEngine.java:95) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.eval(SightlyScriptEngine.java:83)

Seems sling method introspection can't find a getter.

Guessing its div data-sly-list.image="${handle.images }"> . Implement getImages() in your handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM