cancel
Showing results for 
Search instead for 
Did you mean: 

Error while Creating users Using Java

vamsi25
Champ in-the-making
Champ in-the-making
Hi,

ive written a code to create user but iam getting null ptr exception.Below is my code.Please help me where iam going wrong.
Do i need any more configuration to be done.





import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.MutableAuthenticationServiceImpl;
import org.alfresco.repo.security.person.PersonServiceImpl;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;

import org.alfresco.util.PropertyMap;


public class CreateUserToRepository {

   public CreateUserToRepository() {
   }

   private PersonService personService;
   private MutableAuthenticationService authenticationService;
   

      public void createUser(String userName, String password, String firstName,
         String lastName)  {

      
      
         authenticationService = new MutableAuthenticationServiceImpl();
         
         
         if (this.authenticationService.authenticationExists(userName) == false) {

            this.authenticationService.createAuthentication(userName,
                  password.toCharArray());

            PropertyMap personProps = new PropertyMap();
            personProps.put(ContentModel.PROP_USERNAME, userName);
            personProps.put(ContentModel.PROP_FIRSTNAME, firstName);
            personProps.put(ContentModel.PROP_LASTNAME, lastName);
            personProps.put(ContentModel.PROP_EMAIL, userName
                  + "@localhost.com");
            personProps.put(ContentModel.PROP_JOBTITLE, "myJobTitle");
            personProps.put(ContentModel.PROP_JOBTITLE, "myOrganisation");

            personService = new PersonServiceImpl();
            this.personService.createPerson(personProps);

      }
                  
      
   }
   

}


7 REPLIES 7

mrogers
Star Contributor
Star Contributor
Would be helpful if you gave the line number or stack trace but your init of the person and autentication service is nonsense.  You should be injecting your dependencies.

vamsi25
Champ in-the-making
Champ in-the-making
Would be helpful if you gave the line number or stack trace but your init of the person and autentication service is nonsense.  You should be injecting your dependencies.

Hi Mrogers,

iam pretty new to this Smiley Sad iam getting error in object i created authenticationService and person service.
Can you please suggest what dependencies i do require and where to inject them.what correction are required in my code.


Thanks & Regards

vamsi25
Champ in-the-making
Champ in-the-making
We have deployed the alfresco war in unix server and we have developed a struts application and able to connect to it successfully and explore the root and able to create folders and upload files from our localhost.
But not able to create users.What configuration if any needs to be done.
Please help to solve this.

Thanks & Regards

mrogers
Star Contributor
Star Contributor
You can't create the various services directly with "new".

What you should do is add setters/ getter methods to your class then inject the service via spring.

vamsi25
Champ in-the-making
Champ in-the-making
Hi Mrogers,

How to inject the service via spring.Any example?
we are using struts so what files are required in our local application.


Thanks & Regards

nikes
Champ on-the-rise
Champ on-the-rise
Define your class as bean definition in file ending with "-context.xml" and put it in alfresco classpath.

You can look at existing alfresco app for an example.

Within the bean definition, declare all required dependencies, and as mrogers mentioned, create setters/getters for those dependencies in your class.

hope this helps.

nikes
Champ on-the-rise
Champ on-the-rise
Also it looks like you want to perform operations from custom application.

If so, you need to use Alfresco remote API's (WebService or REST (Webscripts)).

Well, REST is lightweight and easier to use than WebServices.