Sightly:为现有方法抛出空指针异常
Sightly : throwing null pointer exception for existing method
我正在尝试创建自己的图像句柄。 class 使用用户定义的 class MySmartImage.java,它具有 com.day.cq.wcm.foundation.Image 和两个字符串属性及其 getter setter,如下所示。
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;
}
}
而我的处理程序如下:
您好,我现在已经在处理程序中按如下方式实现了 getImages。
public class ImageHandler 扩展了 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;
}
}
因此,在我的视觉页面上,我正尝试按如下方式访问图像处理程序。
<div data-sly-use.handle="ImageHandler">
<div data-sly-list.image="${handle.images }">
-----${image.titleText}
</div>
</div>
给出以下异常
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)
似乎 sling 方法自省找不到 getter。
猜猜是 div data-sly-list.image="${handle.images }">
。在您的处理程序中实施 getImages()
。
我正在尝试创建自己的图像句柄。 class 使用用户定义的 class MySmartImage.java,它具有 com.day.cq.wcm.foundation.Image 和两个字符串属性及其 getter setter,如下所示。
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;
}
}
而我的处理程序如下:
您好,我现在已经在处理程序中按如下方式实现了 getImages。
public class ImageHandler 扩展了 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;
}
}
因此,在我的视觉页面上,我正尝试按如下方式访问图像处理程序。
<div data-sly-use.handle="ImageHandler">
<div data-sly-list.image="${handle.images }">
-----${image.titleText}
</div>
</div>
给出以下异常
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)
似乎 sling 方法自省找不到 getter。
猜猜是 div data-sly-list.image="${handle.images }">
。在您的处理程序中实施 getImages()
。