-1

Resource Resolver is being returned as null while injecting through sling models, let me know if anything I am missing :

I tried with :

@Model(adaptables = Resource.class)
public class Navigation {

   @Inject  @Source("sling-object")
   private ResourceResolver resourceResolver;

}

I also tried with:

@Model(adaptables = Resource.class)
public class Navigation {

  @Inject
  private ResourceResolver resourceResolver;

}

In both cases it was being returned as null and throwing a null pointer exception.

Let me know what I may be missing to correct this error.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3837000
  • 121
  • 3
  • 12

2 Answers2

1

This is working for me using the @SlingObject annotation, rather than @Inject and @Source. Theoretically they should be doing the same thing, but figured I'll add this an answer just in case it helps someone else as well.

@Model(adaptables = Resource.class)
public class Navigation {

  @SlingObject
  private ResourceResolver resourceResolver;

}
Jordan Shurmer
  • 946
  • 7
  • 21
0

Check if you have invoked <cq:defineObjects /> before adapting resource to model (<cq:defineObjects /> should be in your global.jsp file which should be included on the beginning of each component)

  • It is being included as copied libs global.jsp into my personal global.jsp and sling models with normal injectors are working fine . Only ResourceResolver is returning null in my case. – user3837000 Aug 26 '15 at 11:21
  • try to add explicitly `` next to the ``. in theory `` should provide objects from `` but imo it is worth to try. may be it doesn't work because you changed `sling` taglib definition uri – Dawid Lewandowski Aug 26 '15 at 19:21
  • added still the same issue persists , any alternative if u can suggest or give inputs – user3837000 Aug 27 '15 at 12:42
  • my last idea it to check what is produced by the `` tag for this purpose you install [**AEM Dash**](https://github.com/Cognifide/AEM-Dash) package which gives possibility to decompile AEM classes Install it, open `/etc/dash.html` page and decompile ` com.day.cq.wcm.tags.DefineObjectsTag` (to see how works `` tag) keep it mind that this class should extend `org.apache.sling.scripting.jsp.taglib.DefineObjectsTag` class so check what it produced by this both classes (the second one should produce resourceResolver object) – Dawid Lewandowski Aug 27 '15 at 14:02
  • Thanks Dawid , but i tried other way using @Self annotation , then adjusting required pom dependencies , so it worked that way . – user3837000 Aug 28 '15 at 04:06
  • 1
    @user3837000 please post an answer describing what you did. It's going to be much easier to find than these comments if anyone else has the same problem. – toniedzwiedz Aug 28 '15 at 20:09