cancel
Showing results for 
Search instead for 
Did you mean: 

Re: How get nodeService in project All-in-One?

javajunior
Champ on-the-rise
Champ on-the-rise

I created project All-in-One by instruction https://github.com/Alfresco/alfresco-sdk/blob/master/docs/getting-started.md

my project https://github.com/LobanTM/All-in-One/tree/master/testartifact

start ./run.sh build_start

go to link  http://localhost:8080/alfresco/service/sample/helloworld

in log you can see 

java.lang.NullPointerException
        at testpackage.platformsample.DemoComponent.getCompanyHome(DemoComponent.java:92)
        at testpackage.platformsample.DemoComponent.executeInternal(DemoComponent.java:62)
        at testpackage.platformsample.HelloWorldWebScript.executeImpl(HelloWorldWebScript.java:57)
        at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:64)
        at org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryContainer.java:519)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:450)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:587)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:656)
        at org.alfresco.repo.web.scripts.RepositoryContainer.executeScriptInternal(RepositoryContainer.java:428)
        at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:308)
...

how get nodes from Alfresco in my project?

thank for any help

1 REPLY 1

abhinavmishra14
World-Class Innovator
World-Class Innovator

The issue is not with generated code, issue is with the code you have written in HelloWorldWebScript class.

DemoComponent class is instantiated using Sprint IOC container and all its dependencies (such as NodeService, NodeLocatorService) are satisfied by the container. 

Now you want to use DemoComponent in your HelloWorldWebScript, you need to inject the testpackage.DemoComponent bean in webscript.alfresco.tutorials.helloworld.get bean in order to use DemoComponent. 

You can not simply create instance as you have done in your webscript class. 

image

When you do this, setter methods in the DemoComponent class has no effect and it wont set any object reference. In this case they become raw java methods. And you may be already aware that if there are instance variables in a class and if you don't set the values/references via any means, then they by default get null value/reference if the object created.

Consider reading this blog for more details on dependency injection: 

https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring

https://www.baeldung.com/spring-dependency-injection

Now coming to the fix, here is what you need to do:

In your HelloWorldWebScript class, see the fix as highlighted:

public class HelloWorldWebScript extends DeclarativeWebScript {
    private static Log logger = LogFactory.getLog(HelloWorldWebScript.class);
    
    private static final String ADMIN_USER_NAME = "admin";
    
    private DemoComponent demo;
    
	public void setDemo(DemoComponent demo) {
		this.demo = demo;
	}

	protected Map<String, Object> executeImpl(
            WebScriptRequest req, Status status, Cache cache) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("fromJava", "HelloFromJava");      
        
        logger.debug("        _____  ___  ____  _____    ");
        logger.debug("  ___  |_   _|| __|/  __||_   _|   ");
        logger.debug(" / _ |   | |  | _| \\__  \\  | |     ");
        logger.debug(" \\___|   |_|  |___||____/  |_|     ");

        model.put("fromAD", "from AD");              
        //logger.debug("-- TEST1 DEMO -- "+ demo);
        
        try {
			demo.executeInternal();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        model.put("fromAlfresco",  model.get("fromJava"));        
        
        logger.debug("Your 'TEST' Web Script was called! =======================");
        
        return model;
    }
}

Update the webscript bean definition:

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="webscript.alfresco.tutorials.helloworld.get"
		  class="testpackage.platformsample.HelloWorldWebScript"
		  parent="webscript">
		  <property name="demo" ref="testpackage.DemoComponent" />
	</bean>
</beans>
~Abhinav
(ACSCE, AWS SAA, Azure Admin)