Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Jamie Grant
Add User - addDefaultGroups NPE
April 22, 2013 9:44 AM
Answer

Jamie Grant

Rank: New Member

Posts: 7

Join Date: October 25, 2012

Recent Posts

I'm having some difficulties programattically adding a user.

My src code lines aren't lining up exactly with my debugger, so it's a little difficult to tell exactly what's going on, but the problem seems to come from the UserLocalServiceImpl.addDefaultGroups() method.

In the last line of this method it calls:

1groupLocalService.addUserGroups(userId, groupIds);


GroupIds is null, because there aren't any new groups with the new user I'm creating - it then throws a npe.

Can groupIds be null when creating a new user? Or is the bug in UserPersistenceImpl when it tries to loop over a group id array that is null?

thanks for the help

Liferay 6.1

 1
 2
 3*** com.liferay.portal.service.impl.UserLocalServiceImpl ****
 4
 5public void addDefaultGroups(long userId) throws PortalException, SystemException {
 6        User user = userPersistence.findByPrimaryKey(userId);
 7        Set<Long> groupIdsSet = new HashSet<Long>();
 8        String[] defaultGroupNames = PrefsPropsUtil.getStringArray(
 9            user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_GROUP_NAMES,
10            StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_GROUP_NAMES);
11        for (String defaultGroupName : defaultGroupNames) {
12            Company company = companyPersistence.findByPrimaryKey(
13                user.getCompanyId());
14
15            Account account = company.getAccount();
16
17            if (defaultGroupName.equalsIgnoreCase(account.getName())) {
18                defaultGroupName = GroupConstants.GUEST;
19            }
20
21            try {
22                Group group = groupPersistence.findByC_N(
23                    user.getCompanyId(), defaultGroupName);
24
25                if (!userPersistence.containsGroup(
26                        userId, group.getGroupId())) {
27
28                    groupIdsSet.add(group.getGroupId());
29                }
30            }
31            catch (NoSuchGroupException nsge) {
32            }
33        }
34
35        long[] groupIds = ArrayUtil.toArray(
36            groupIdsSet.toArray(new Long[groupIdsSet.size()]));
37
38        groupLocalService.addUserGroups(userId, groupIds);
39    }


 1
 2java.lang.NullPointerException
 3    at com.liferay.portal.service.impl.UserLocalServiceImpl.addUserWithWorkflow(UserLocalServiceImpl.java:742)
 4    at com.liferay.portal.service.impl.UserLocalServiceImpl.addUser(UserLocalServiceImpl.java:463)
 5    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 6    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 7    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 8    at java.lang.reflect.Method.invoke(Method.java:597)
 9    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:112)
10    at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:71)
11    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:108)
12    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:59)
13    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:108)
14    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:59)
15    at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:108)
16    at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:211)
17    at $Proxy96.addUser(Unknown Source)
18    at com.liferay.portal.service.UserLocalServiceUtil.addUser(UserLocalServiceUtil.java:445)
Jamie Grant
RE: Add User - addDefaultGroups NPE
April 26, 2013 9:43 AM
Answer

Jamie Grant

Rank: New Member

Posts: 7

Join Date: October 25, 2012

Recent Posts

Solved. Although I had almost the exact same code as com.liferay.portal.service.UserServiceTest, ServiceContext was null, and resulted in the error above.

From com.liferay.portal.service.UserServiceTest
 1
 2protected User addUser() throws Exception {
 3        boolean autoPassword = true;
 4        String password1 = StringPool.BLANK;
 5        String password2 = StringPool.BLANK;
 6        boolean autoScreenName = true;
 7        String screenName = StringPool.BLANK;
 8        String emailAddress =
 9            "UserServiceTest." + ServiceTestUtil.nextLong() + "@liferay.com";
10        long facebookId = 0;
11        String openId = StringPool.BLANK;
12        Locale locale = LocaleUtil.getDefault();
13        String firstName = "UserServiceTest";
14        String middleName = StringPool.BLANK;
15        String lastName = "UserServiceTest";
16        int prefixId = 0;
17        int suffixId = 0;
18        boolean male = true;
19        int birthdayMonth = Calendar.JANUARY;
20        int birthdayDay = 1;
21        int birthdayYear = 1970;
22        String jobTitle = StringPool.BLANK;
23        long[] groupIds = null;
24        long[] organizationIds = null;
25        long[] roleIds = null;
26        long[] userGroupIds = null;
27        boolean sendMail = false;
28
29        ServiceContext serviceContext = new ServiceContext();
30
31        return UserServiceUtil.addUser(
32            TestPropsValues.getCompanyId(), autoPassword, password1, password2,
33            autoScreenName, screenName, emailAddress, facebookId, openId,
34            locale, firstName, middleName, lastName, prefixId, suffixId, male,
35            birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
36            organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
37    }