简体   繁体   English

为什么在其他类(Java / apache吊索)中使用时,父类的字段为何具有空值

[英]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 ListModelMyNextListModel ,但是即使在第二个类中它仍然为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.

相关问题 当使用Java中的多态性从父类打印子私有字段时,为什么子私有字段为null? - Why child private field is null when printing it from parent class using polymorphism in java? org.apache.sling.api.scripting.SlingScriptHelper.getService(Class <ServiceType> )返回null? VerifyError:需要一个堆栈映射框架 - When does org.apache.sling.api.scripting.SlingScriptHelper.getService(Class<ServiceType>) returns null? VerifyError: Expecting a stackmap frame 如何从其他 java 类中检索字段值? - How do I retrieved field value from other java class? 尝试从其他类检索时的空值 - Null value when trying to retrieve from other class 为什么从父类打印字段,从子类打印方法(Java) - Why field is printed from parent class, and method from children class (Java) 为什么子类对象在Java中调用父类字段变量? - Why is the child class object invoking the parent class field variable in Java? Java继承和后期绑定,为什么int id具有父类值而不是子类1? - Java Inheritance and late binding, why does int id have the parent class value and not the child class one? 为什么从另一个Java Servlet或Java类调用Java Servlet类中的方法会返回null? - Why method in Java servlet class return null when called from another java servlet or java class? Java无法识别其他类的字段 - Java not recognising field from other class 为什么对象引用的是父类的值而不是指派给它的类? - Why does the object refers to the value of the parent class instead of the class it is assigned to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM