In my java
code I have the following class:
@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.
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?
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
If the class that is inheriting from your model is missing the @model annotation, sling does not know what to do. 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.