掲示板

Add User - addDefaultGroups NPE

10年前 に Jamie Grant によって更新されました。

Add User - addDefaultGroups NPE

New Member 投稿: 7 参加年月日: 12/10/25 最新の投稿
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:

groupLocalService.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



*** com.liferay.portal.service.impl.UserLocalServiceImpl ****

public void addDefaultGroups(long userId) throws PortalException, SystemException {
		User user = userPersistence.findByPrimaryKey(userId);
		Set<long> groupIdsSet = new HashSet<long>();
		String[] defaultGroupNames = PrefsPropsUtil.getStringArray(
			user.getCompanyId(), PropsKeys.ADMIN_DEFAULT_GROUP_NAMES,
			StringPool.NEW_LINE, PropsValues.ADMIN_DEFAULT_GROUP_NAMES);
		for (String defaultGroupName : defaultGroupNames) {
			Company company = companyPersistence.findByPrimaryKey(
				user.getCompanyId());

			Account account = company.getAccount();

			if (defaultGroupName.equalsIgnoreCase(account.getName())) {
				defaultGroupName = GroupConstants.GUEST;
			}

			try {
				Group group = groupPersistence.findByC_N(
					user.getCompanyId(), defaultGroupName);

				if (!userPersistence.containsGroup(
						userId, group.getGroupId())) {

					groupIdsSet.add(group.getGroupId());
				}
			}
			catch (NoSuchGroupException nsge) {
			}
		}

		long[] groupIds = ArrayUtil.toArray(
			groupIdsSet.toArray(new Long[groupIdsSet.size()]));

		groupLocalService.addUserGroups(userId, groupIds);
	}
</long></long>



java.lang.NullPointerException
	at com.liferay.portal.service.impl.UserLocalServiceImpl.addUserWithWorkflow(UserLocalServiceImpl.java:742)
	at com.liferay.portal.service.impl.UserLocalServiceImpl.addUser(UserLocalServiceImpl.java:463)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:112)
	at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:71)
	at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:108)
	at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:59)
	at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:108)
	at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:59)
	at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:108)
	at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:211)
	at $Proxy96.addUser(Unknown Source)
	at com.liferay.portal.service.UserLocalServiceUtil.addUser(UserLocalServiceUtil.java:445)
10年前 に Jamie Grant によって更新されました。

RE: Add User - addDefaultGroups NPE

New Member 投稿: 7 参加年月日: 12/10/25 最新の投稿
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

protected User addUser() throws Exception {
		boolean autoPassword = true;
		String password1 = StringPool.BLANK;
		String password2 = StringPool.BLANK;
		boolean autoScreenName = true;
		String screenName = StringPool.BLANK;
		String emailAddress =
			"UserServiceTest." + ServiceTestUtil.nextLong() + "@liferay.com";
		long facebookId = 0;
		String openId = StringPool.BLANK;
		Locale locale = LocaleUtil.getDefault();
		String firstName = "UserServiceTest";
		String middleName = StringPool.BLANK;
		String lastName = "UserServiceTest";
		int prefixId = 0;
		int suffixId = 0;
		boolean male = true;
		int birthdayMonth = Calendar.JANUARY;
		int birthdayDay = 1;
		int birthdayYear = 1970;
		String jobTitle = StringPool.BLANK;
		long[] groupIds = null;
		long[] organizationIds = null;
		long[] roleIds = null;
		long[] userGroupIds = null;
		boolean sendMail = false;

		ServiceContext serviceContext = new ServiceContext();

		return UserServiceUtil.addUser(
			TestPropsValues.getCompanyId(), autoPassword, password1, password2,
			autoScreenName, screenName, emailAddress, facebookId, openId,
			locale, firstName, middleName, lastName, prefixId, suffixId, male,
			birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
			organizationIds, roleIds, userGroupIds, sendMail, serviceContext);
	}