# Attribute Generator Rule throwing Null Pointer exception error

**URL:** <https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273>\
**Category:** SHF Discussion and Questions\
**Tags:** rules, sailpoint-identity-security, provisioning\
**Created:** [February 25, 2024, 11:19pm UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273 "2024-02-25T23:19:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgrant](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mgrant](https://developer.sailpoint.com/discuss/u/mgrant)\
**Post date:** [February 25, 2024, 11:19pm UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/1 "2024-02-25T23:19:20Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![DeepanshuBisht](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/deepanshubisht/32/6848_2.png) [@DeepanshuBisht](https://developer.sailpoint.com/discuss/u/DeepanshuBisht)\
**Post date:** [February 26, 2024, 5:47am UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/2 "2024-02-26T05:47:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RAKGDS](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/rakgds/32/2795_2.png) [@RAKGDS](https://developer.sailpoint.com/discuss/u/RAKGDS)\
**Post date:** [February 26, 2024, 9:17am UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/3 "2024-02-26T09:17:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mgrant](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mgrant](https://developer.sailpoint.com/discuss/u/mgrant)\
**Post date:** [February 26, 2024, 9:26pm UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/4 "2024-02-26T21:26:27Z")

</div>

Hi Rakesh/Deepanshu,

Code is below

```auto
<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>

```

---

<div class="post-metadata">

**Author:** ![RAKGDS](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/rakgds/32/2795_2.png) [@RAKGDS](https://developer.sailpoint.com/discuss/u/RAKGDS)\
**Post date:** [February 27, 2024, 5:22am UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/5 "2024-02-27T05:22:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baoussounda](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/baoussounda/32/13095_2.png) [@baoussounda](https://developer.sailpoint.com/discuss/u/baoussounda)\
**Post date:** [February 27, 2024, 7:58am UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/6 "2024-02-27T07:58:40Z")

</div>

> [@mgrant](#):
>
> `ule>`

Hi Marwin,

As you can see in your code :

```auto
 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 :

```auto
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 :

```auto
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

---

<div class="post-metadata">

**Author:** ![DeepanshuBisht](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/deepanshubisht/32/6848_2.png) [@DeepanshuBisht](https://developer.sailpoint.com/discuss/u/DeepanshuBisht)\
**Post date:** [February 27, 2024, 8:16am UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/7 "2024-02-27T08:16:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Prashansa](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Prashansa](https://developer.sailpoint.com/discuss/u/Prashansa)\
**Post date:** [February 27, 2024, 8:35am UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/8 "2024-02-27T08:35:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mgrant](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mgrant](https://developer.sailpoint.com/discuss/u/mgrant)\
**Post date:** [February 27, 2024, 12:17pm UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/9 "2024-02-27T12:17:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sailpoint/original/2X/f/f2136700ed5e3703e0b85e02f6be799dacca7735.png) [@system](https://developer.sailpoint.com/discuss/u/system)\
**Post date:** [April 27, 2024, 12:18pm UTC](https://developer.sailpoint.com/discuss/t/attribute-generator-rule-throwing-null-pointer-exception-error/32273/10 "2024-04-27T12:18:26Z")

</div>

Closed on April 27, 2024, 12:18pm UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
