[英]Why does the field from parent class has a null value when using in other class (Java/apache sling)
In my java
code I have the following class: 在我的java
代码中,我有以下类别:
@Model(adaptables = SlingHttpServletRequest.class)
public class ListModel extends SomeList{
@Inject
protected ResourceResolver resourceResolver;
//this method works fine:
public List<String> getAllData(){
System.out.println(resourceResolver.getUserID());
}
}
When I call method getAllData()
from this class, I see my user's id - which is fine. 当我从此类调用方法getAllData()
,我看到了用户的getAllData()
很好。
Now I need to write a 2nd class that will extend the one above. 现在,我需要编写一个第二类,将扩展上面的一个。 It looks like this: 看起来像这样:
public class MyNextListModel extends ListModel{
//this method throws nullpointer because resourceResolver is null - why?
@Override
public List<ListItemModel> getAllItems() {
System.out.println(resourceResolver.getUserID());
}
}
How come the resourceResolver in the 2nd class has a null value? 为什么第二类中的resourceResolver具有空值?
I also tried moving this code: 我也尝试移动此代码:
@Inject
protected ResourceResolver resourceResolver;
from ListModel
to MyNextListModel
, but even though, it's still null in the 2nd class 从ListModel
到MyNextListModel
,但是即使在第二个类中它仍然为null
If the class that is inheriting from your model is missing the @model annotation, sling does not know what to do. 如果从您的模型继承的类缺少@model批注,则sling不知道该怎么做。 Simply add the annotation to the inheriting class and your code should work. 只需将注释添加到继承的类,您的代码就可以工作。 Btw make sure the the other class is also in a package that is registered as sling models package in your core Pom. 顺便说一句,确保另一个类也位于您的核心Pom中注册为吊索模型的软件包中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.