ReplacingCallback handler throwing null pointer when trying to access a different service

Hi I am getting a null pointer exception when running a custom replacingHandler. I will post the code that is using the replacer and the ReplaceHandler code

Here is the handler

public class ReplaceWithBeneTableHandler implements IReplacingCallback {

  public PartyRestService partyRestService;

  @Override
  public int replacing(ReplacingArgs e) throws Exception {

    Node currentNode = e.getMatchNode();


    DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
    builder.moveTo(currentNode);
    builder.startTable();
    String agreementNumber = e.getReplacement();

    List<PartyDto> parties = partyRestService.executePartyGet(agreementNumber);

    builder.startTable();

    for (PartyDto party : parties) {

      if (party.getPartyRoleCode().contains("PRIM_BENE")) {

        builder.insertCell();
        if (party.getFirstName() != null && party.getLastName() != null) {
          builder.write(party.getFirstName() + party.getLastName());
        } else if (party.getFirstName() == null && party.getLastName() != null) {
          builder.write(party.getLastName());
        } else if (party.getFirstName() != null && party.getLastName() == null) {
          builder.write(party.getFirstName());
        }


        builder.insertCell();
        builder.write(party.getBeneficiarySplitRate().toString());
        builder.endRow();
      }
    }


    builder.endTable();

    builder.startTable();

    for (PartyDto party : parties) {

      if (party.getPartyRoleCode().contains("CON_BENE")) {

        builder.insertCell();
        if (party.getFirstName() != null && party.getLastName() != null) {
          builder.write(party.getFirstName() + party.getLastName());
        } else if (party.getFirstName() == null && party.getLastName() != null) {
          builder.write(party.getLastName());
        } else if (party.getFirstName() != null && party.getLastName() == null) {
          builder.write(party.getFirstName());
        }


        builder.insertCell();
        builder.write(party.getBeneficiarySplitRate().toString());
        builder.endRow();
      }
    }


    builder.endTable();


    currentNode.remove();
    return ReplaceAction.SKIP;
  }

}

Here is where the code is being called to replace.

  public Document replaceBeneTableTags(Document preDoc, String agreementNumber) {
    Document updatedDoc = preDoc;

    FindReplaceOptions beneTableOpts = new FindReplaceOptions();
    beneTableOpts.setReplacingCallback(new ReplaceWithBeneTableHandler());
    beneTableOpts.setDirection(FindReplaceDirection.BACKWARD);

    try {

      if (updatedDoc.getText().contains("+BENE_TABLE+")) {
        updatedDoc.getRange().replace("+BENE_TABLE+", agreementNumber, beneTableOpts);
      }
    } catch (Exception e) {
      System.out.println(e);
    }
    return updatedDoc;
  }

Any help would be appreciated. I get the null pointer at the

List<PartyDto> parties = partyRestService.executePartyGet(agreementNumber); 

line

@Ebaldwin174

It looks like you’re encountering a NullPointerException in your custom ReplacingCallback handler when trying to access the partyRestService. This issue typically arises when the partyRestService instance is not initialized before it is used in the replacing method.

To resolve this, you need to ensure that the partyRestService is properly instantiated before you call executePartyGet(agreementNumber). Here are a few suggestions to fix the issue:

  1. Initialize the Service: Make sure that you are initializing the partyRestService in the constructor of your ReplaceWithBeneTableHandler class. For example:

    public class ReplaceWithBeneTableHandler implements IReplacingCallback {
        public PartyRestService partyRestService;
    
        public ReplaceWithBeneTableHandler(PartyRestService partyRestService) {
            this.partyRestService = partyRestService;
        }
    
        @Override
        public int replacing(ReplacingArgs e) throws Exception {
            // Your existing code...
        }
    }
    
  2. Pass the Service Instance: When you create an instance of ReplaceWithBeneTableHandler, make sure to pass the initialized partyRestService:

    public Document replaceBeneTableTags(Document preDoc, String agreementNumber, PartyRestService partyRestService) {
        Document updatedDoc = preDoc;
    
        FindReplaceOptions beneTableOpts = new FindReplaceOptions();
        beneTableOpts.setReplacingCallback(new ReplaceWithBeneTableHandler(partyRestService));
        beneTableOpts.setDirection(FindReplaceDirection.BACKWARD);
    
        try {
            if (updatedDoc.getText().contains("+BENE_TABLE+")) {
                updatedDoc.getRange().replace("+BENE_TABLE+", agreementNumber, beneTableOpts);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    
        return updatedDoc;
    }
    
  3. Check for Null: Before calling executePartyGet, you can also add a null check to handle cases where partyRestService might not be initialized:

    if (partyRestService == null) {
        throw new IllegalStateException("PartyRestService is not initialized.");
    }
    List<PartyDto> parties = partyRestService.executePartyGet(agreementNumber);
    

By ensuring that partyRestService is properly initialized and passed to your handler, you should be able to avoid the NullPointerException. If you continue to experience issues, please provide additional details about how partyRestService is being instantiated and passed around in your code.

If you have any further questions or need additional assistance, feel free to ask!

@Ebaldwin174 Could you please also attach your problematic input document here for testing? We will check your scenario and provide you more information.