如何使露天的以下代码正常工作?

时间:2016-11-10 11:40:57

标签: java alfresco

我在Alfresco中创建了一个名为CreateUserHandler的类,以便为电子邮件地址生成用户名和随机密码。我在这方面有例外......请有人帮我这个......

add_filter( "woocommerce_variable_sale_price_html", "theanand_remove_prices", 10, 2 );
add_filter( "woocommerce_variable_price_html", "theanand_remove_prices", 10, 2 );
add_filter( "woocommerce_get_price_html", "theanand_remove_prices", 10, 2 );

function theanand_remove_prices( $price, $product ) {
    if ( ! $product->is_in_stock()) {
        $price = "";
    }
    return $price;
}

我还附加了空指针异常的日志堆栈跟踪:

public class CreateUserHandler {
     private static Logger logger = LoggerFactory.getLogger(CreateUserHandler.class);
     private NodeService nodeService = getServiceRegistry().getNodeService();
     private PersonService personService = getServiceRegistry()
                .getPersonService();
     private ActionService actionService = getServiceRegistry()
                .getActionService();
     private SearchService searchService = getServiceRegistry()
                .getSearchService();

        private PolicyComponent eventManager;
        private ServiceRegistry serviceRegistry;
        //private String userName;

        public void setServiceRegistry(ServiceRegistry serviceRegistry) {
            this.serviceRegistry = serviceRegistry;
        }

        private ServiceRegistry getServiceRegistry() {
            // TODO Auto-generated method stub
            return null;
        }

        public void setPolicyComponent(PolicyComponent policyComponent) {
            this.eventManager = policyComponent;
        }

        public void registerEventHandlers() {
            eventManager.bindClassBehaviour(
                    NodeServicePolicies.OnCreateNodePolicy.QNAME,
                    ContentModel.TYPE_USER,
                    new JavaBehaviour(this, "onaddUser",
                            Behaviour.NotificationFrequency.EVERY_EVENT));


        }
        protected String userName = null;
        protected String password = null;
        /**
         * Get user password
         * @return user password
         */
        public String getPassword() {
           return password;
        }

        /**
         * Set user password
         * @param password user password
         */
        public void setPassword(String password) {
           this.password = password;
        }


        public void onaddUser(ChildAssociationRef parentChildAssocRef) {
            //new PersonService().setPersonProperties(userName, properties);
            System.out.println("Creation of user successfully completed");

            //NodeRef userNodeRef;
            // String sTempUserName = this.userName;
            NodeRef person = null;
             String username = (String) serviceRegistry.getNodeService().getProperty(person,ContentModel.PROP_USERNAME);



                     System.out.println("Username got :" + username);

                      if (personService.personExists(username)) {

                            //NodeRef person = personService.getPerson(sTempUserName);
                          person = personService.getPerson(username);

                            String address = (String) nodeService.getProperty(person,

                                        ContentModel.PROP_EMAIL);

                            if (address != null && address.length() != 0) {

                                  // recipients.add(address);

                                  System.out.println("Email Address is :" + address);
                                  String newPassword = Password.generatePassword();

                                  char[] cadChars = new char[newPassword.length()];

                                  for (int i=0; i<newPassword.length(); i++) 
                                  {
                                     cadChars[i] = newPassword.charAt(i);
                                  }
                                  // Save new password 
                                  serviceRegistry.getAuthenticationService().setAuthentication(username, newPassword.toCharArray());
                                  // Save new password
                                   setPassword(newPassword);
                                   System.out.println("Password is :" + newPassword);

                            }

                      }
                }




}

2 个答案:

答案 0 :(得分:3)

这可能是因为

    private ServiceRegistry getServiceRegistry() {
        // TODO Auto-generated method stub
        return null;
    }

 private PersonService personService = getServiceRegistry()
            .getPersonService();

结果NPE被抛出。

答案 1 :(得分:0)

private NodeService nodeService = getServiceRegistry().getNodeService();
如果getServiceRegistry()始终返回null

将无效。

最简单的方法是删除成员nodeService并更改

之类的行
String address = (String) nodeService.getProperty(person, ContentModel.PROP_EMAIL);

String address = (String) serviceRegistry.getNodeService().getProperty(person, ContentModel.PROP_EMAIL);

personServiceactionServicesearchService执行相同操作。