Can't get a template from confluence with PageTemplateManager

I’m trying to get a template’s XML in my addon.
I’m using PageTemplateManager to get the template by name.
I’m unable to get the PageTemplateManager component:

        String templateName = URLDecoder.decode(name,"UTF-8");
        PageTemplateManager pageTemplateManager = ComponentLocator.getComponent(PageTemplateManager.class);
        String template = pageTemplateManager.getPageTemplate(templateName,null).getContent();

I’m getting an error:

java.lang.NullPointerException
at com.atlassian.sal.api.component.ComponentLocator.getComponent

hen trying the same with scriptrunner’s script console I get the XML fine

All the templates in confluence are associated with space, so get exact template you need to pass the space.
See method in Java Doc -
https://docs.atlassian.com/atlassian-confluence/5.9.3/com/atlassian/confluence/pages/templates/PageTemplateManager.html#getPageTemplate-java.lang.String-com.atlassian.confluence.spaces.Space-

String templateName = URLDecoder.decode(name,“UTF-8”);
PageTemplateManager pageTemplateManager = ComponentLocator.getComponent(PageTemplateManager.class);
String template = pageTemplateManager.getPageTemplate(templateName,null).getContent();

null you put there is wrong pass the space of template.

I don’t think this is the issue: Exception is thrown from ComponentLocator.
“null” should indicate the template is global.
Anyway, this doesn’t work even with template ID

I would assume that you are inject PageTemplateManager something like this way -

private final PageTemplateManager pageTemplateManager;
 @Inject
 public classNameConstructor(@ComponentImport PageTemplateManager pageTemplateManager) {
     this.pageTemplateManager = pageTemplateManager;
 }

 @Override
 public void methodName(long contentId) {
     List templateList = pageTemplateManager.getGlobalPageTemplates(templateName);

This should work. Let me know if you have any problems implementing this.