2017-08-18 32 views
0

在我java代码,我有下面的类使用时有一个空值,我看到了我的用户ID - 这很好。为什么从父类确实实地在其他类(Java/Apache的吊索)

现在我需要编写一个扩展上面的第二个类。它看起来像这样:

public class MyNextListModel extends ListModel{ 

    //this method throws nullpointer because resourceResolver is null - why? 
    @Override 
    public List<ListItemModel> getAllItems() { 
     System.out.println(resourceResolver.getUserID()); 
    } 
} 

为什么第二个类中的resourceResolver有一个空值?

我也尝试移动此代码:从ListModel

@Inject 
protected ResourceResolver resourceResolver; 

MyNextListModel,但即便如此,它仍然是空的二等

+0

你试过super.resourceResolver.getUserID()吗? –

+0

凯尔,我试过了,它不工作,不幸的是... – randomuser1

+0

我现在没有办法检查这个,但我相信Sling需要子类本身用'@ Model'注释进行注释。它不是在超级课堂上寻找它。尝试添加'@Model(adaptables = SlingHttpServletRequest.class)'到'MyNextListModel'。 – toniedzwiedz

回答

1

如果从你的模型继承类缺少@model注释,吊索不知道该怎么做。只需将注释添加到继承类,并且您的代码应该可以工作。顺便说一句,确保其他课程也在一个包装中,它被注册为核心Pom中的吊索模型包。

相关问题