Attribute Generator Rule throwing Null Pointer exception error

Team,

We are testing an Attribute Generator Rule and it is throwing the error below. Does this mean there is a coding error in the rule?

trackingId: 1ac7af434caf4171a437cb89e5711baa java.lang.RuntimeException: sailpoint.tools.GeneralException: Error running rule transform:sailpoint.tools.GeneralException: The application script threw an exception: java.lang.NullPointerException: Null Pointer in Method Invocation BSF info: Attribute Generator Historical Name Check at line: 0 column: columnNo

Hi Marvin,
can you share information regarding this like the code snippet etc and your requirements.

Hi Marvin,
There is a null pointer exception thrown from the code itself. Please check the code and see if you are missing any value which may result in null pointer exception or share the code here we might be able to help you

Thanks
Rakesh

Hi Rakesh/Deepanshu,

Code is below

<Rule language="beanshell" name="Attribute Generator Historical Name Check" type="AttributeGenerator">

<Description>Check for available username in Flat File source</Description>

<Source>

<![CDATA[ import sailpoint.tools.GeneralException; import java.util.Iterator; import sailpoint.object.*; import java.util.ArrayList; import sailpoint.api.*; import sailpoint.object.*; import sailpoint.rule.*; import java.util.Iterator; import java.util.List; import org.apache.commons.lang.StringUtils; int maxIteration = 50; log.info("Attribute Generator Rule Started:" + maxIteration); public String generateusername(String firstName,String lastName,String middleInitial,int iteration){ log.info("Attribute Generator Rule:" + middleInitial); log.info("Attribute Generator Rule: In generateusername method"); // Data protection firstName = StringUtils.trimToNull(firstName.replaceAll("[^a-zA-Z]+", "")); log.info("Attribute Generator Rule:" + firstName); if(null!=middleInitial){ middleInitial = StringUtils.trimToNull(middleInitial.replaceAll("[^a-zA-Z]+", "").substring(0,1)); log.info("Attribute Generator Rule:middleinitial" + middleInitial); } lastName = StringUtils.trimToNull(lastName.replaceAll("[^a-zA-Z]+", "")); log.info("Attribute Generator Rule:Lastname" + lastName); if((firstName == null) || (lastName == null)) return null; // This will hold the final username; String username = ""; switch(iteration){ case 0: username = firstName.substring(0, 1) + lastName; log.info("Attribute Generator Rule:username case 0" + username); break; case 1: if(null!=middleInitial){ username = firstName.substring(0, 1) + middleInitial + lastName; log.info("Attribute Generator Rule:Middle" + username); } break; case 2: for (int i = 2; i <= firstName.length(); i++) { username = firstName.substring(0, i) + lastName; log.info("Attribute Generator Rule:" + username ); if(isUnique(username)) { return username; } } break; default: username = firstName.substring(0, 1) + lastName + (iteration - 2); log.info("Attribute Generator Rule:Iteration" + username); break; } if(null != username && !(username.isEmpty()) && isUnique(username)) { log.info("Attribute Generator Rule:In If block" + username); return username; } else if(iteration < maxIteration) { log.info("Attribute Generator Rule:In Iteration block" + username); return generateusername(firstName, lastName, middleInitial, (iteration + 1)); } else { log.info("Attribute Generator Rule:In Null Block" + username); return null; } } public boolean isUnique(String username) { //throws GeneralException { boolean result = !idn.accountExistsByNativeIdentity("Historical Usernames [source]", username); log.info("Attribute Generator Rule: Result from comparison" + result); return result; } log.info("Attribute Generator Rule Starts now"); return generateusername(identity.getAttribute("firstname"),identity.getAttribute("lastname"),identity.getAttribute("middleName"),0); ]]>

</Source>

</Rule>

Hi Marvin,
Can you make sure that middleName,fisrtName and lastName are populated with values?
Thanks

Hi Marwin,

As you can see in your code :

 import sailpoint.tools.GeneralException; 
 import java.util.Iterator; 
 import sailpoint.object.*;
 import java.util.ArrayList; 
 import sailpoint.api.*; 
 import sailpoint.object.*; 
 import sailpoint.rule.*; 
 import java.util.Iterator; 
 import java.util.List; 
 import org.apache.commons.lang.StringUtils; 
 int maxIteration = 50; 
 log.info("Attribute Generator Rule Started:" + maxIteration); 
 public String generateusername(String firstName,String lastName,String middleInitial,int iteration){
     log.info("Attribute Generator Rule:" + middleInitial); 
     log.info("Attribute Generator Rule: In generateusername method"); 
    // Data protection firstName = StringUtils.trimToNull(firstName.replaceAll("[^a-zA-Z]+", "")); 
    log.info("Attribute Generator Rule:" + firstName); 
    if(null!=middleInitial){
        middleInitial = StringUtils.trimToNull(middleInitial.replaceAll("[^a-zA-Z]+", "").substring(0,1)); 
        log.info("Attribute Generator Rule:middleinitial" + middleInitial); 
    }    
    lastName = StringUtils.trimToNull(lastName.replaceAll("[^a-zA-Z]+", "")); 
    log.info("Attribute Generator Rule:Lastname" + lastName); 
    if((firstName == null) || (lastName == null)) return null; 
 // This will hold the final username; 
 String username = ""; 
 switch(iteration){
     case 0: username = firstName.substring(0, 1) + lastName; 
    log.info("Attribute Generator Rule:username case 0" + username); 
 break; 
 case 1: if(null!=middleInitial){
     username = firstName.substring(0, 1) + middleInitial + lastName; 
    log.info("Attribute Generator Rule:Middle" + username); 
} break; 
 case 2: for (int i = 2; 
 i <= firstName.length(); 
 i++) {
     username = firstName.substring(0, i) + lastName; 
    log.info("Attribute Generator Rule:" + username ); 
 if(isUnique(username)) {
     return username; 
} } break; 
 default: username = firstName.substring(0, 1) + lastName + (iteration - 2); 
 log.info("Attribute Generator Rule:Iteration" + username); 
 break; 
} if(null != username && !(username.isEmpty()) && isUnique(username)) {
     log.info("Attribute Generator Rule:In If block" + username); 
 return username; 
} else if(iteration < maxIteration) {
     log.info("Attribute Generator Rule:In Iteration block" + username); 
 return generateusername(firstName, lastName, middleInitial, (iteration + 1)); 
} else {
     log.info("Attribute Generator Rule:In Null Block" + username); 
 return null; 
} } 

public boolean isUnique(String username) {
     //throws GeneralException {
     boolean result = !idn.accountExistsByNativeIdentity("Historical Usernames [source]", username); 
        log.info("Attribute Generator Rule: Result from comparison" + result); 
        return result; 

    } 

    log.info("Attribute Generator Rule Starts now"); 
 return generateusername(identity.getAttribute("firstname"),identity.getAttribute("lastname"),identity.getAttribute("middleName"),0);

In your generateusername methode :

you do :

lastName = StringUtils.trimToNull(lastName.replaceAll("[^a-zA-Z]+", "")); 
    log.info("Attribute Generator Rule:Lastname" + lastName); 
    if((firstName == null) || (lastName == null)) return null; 

if your lastName is null, this can occured null pointer exception.

You can do something like this :

lastName = StringUtils.trimToNull(lastName); 
    log.info("Attribute Generator Rule:Lastname" + lastName); 
    if((firstName == null) || (lastName == null)) return null; 
if(null != lastName) {
  lastName = lastName.replaceAll("[^a-zA-Z]+", "")
}

You can do same for firstName and middleInitial

Hi Marvin,

The error you’re encountering, java.lang.NullPointerException, suggests that somewhere in your code, you’re trying to invoke a method or access a property on a null object reference.

In your code, one possible source of this error could be when you’re trying to access attributes from the identity object. If identity is null or any of its attributes (firstname, lastname, middleName) are null, it could lead to a NullPointerException when trying to invoke methods or access properties on them.

To troubleshoot this issue, you should add null checks before accessing attributes from the identity object. For example:

if (identity != null && identity.getAttribute(“firstname”) != null && identity.getAttribute(“lastname”) != null && identity.getAttribute(“middleName”) != null) {
return generateusername(identity.getAttribute(“firstname”), identity.getAttribute(“lastname”), identity.getAttribute(“middleName”), 0);
} else {
// Handle the case where one of the attributes is null
}

Regards,
Deepanshu Bisht

Hi Marvin,

In your code, you are calling the replaceAll() method on a variable which could be null(lastName, of middleInitial).
Try passing hardcoded values in your code for firstname, lastname and middleName and test. If that works for one user, you can put null checks in place then.

Thanks,
Prash

Hi Prashansa,Deepanshu,Ousmane and Rakesh,

Thank you so much for the input. This is very valuable and enlightening. Let me give your suggestions a go. I’ll keep you posted.

3 Likes