[SalesForce] Having trouble writing Apex trigger on custom object to update a lookup fields

I have a trigger that maps information from one Custom Object (Agents) to another Custom Object (Trident Contracts). I am able to update Seller Agent Name in the Trident Contract Mapping with the ID. see code below

trigger SellAgent on Trident_Contract__c (before insert, before update) {
    Set <String> AgentID = new Set <String>();
    for (Trident_Contract__c tc : trigger.new) {
        if (tc.Seller_Agent_ID__c != NULL) {
            AgentID.add(tc.Seller_Agent_ID__c);
        }
    }
    Map<String, Agent__c> Agent = new map <String, Agent__C>();
    for (Agent__c obj : [SELECT ID, Agent_ID__c,
                         from Agent__c
                         Where Agent_ID__c
                         IN: AgentID]) {
        Agent.put(obj.Agent_ID__c, obj);
    }
    for (Trident_Contract__c tc : trigger.new) {
        if (Agent.containsKey(tc.Seller_Agent_ID__c)) tc.Seller_Agent_Lookup__c = Agent.get(tc.Seller_Agent_ID__c).ID;
    }
}

Now I want to add to this trigger and update another field on the Trident Contract (Seller Agent Office)using a map from the Agent (office) field. But I get this error message when I run the test

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AgentInfo: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de – reference a null object
Trigger.AgentInfo: line 20, column 1: []

Here is my updated code and Test. Please let me know what I am doing wrong.

trigger SellAgent on Trident_Contract__c (before insert, before update) {
    Set <String> AgentID = new Set <String>();
    for (Trident_Contract__c tc : trigger.new) {
        if (tc.Seller_Agent_ID__c != NULL) {
            AgentID.add(tc.Seller_Agent_ID__c);
        }
    }
    Map<String, Agent__c> Agent = new map <String, Agent__c>();
    for (Agent__c obj : [SELECT ID, Agent_ID__c, Agent_Office__c
                         from Agent__c
                         Where Agent_ID__c
                         IN: AgentID]) {
        Agent.put(obj.Agent_ID__c, obj);
    }
    for (Trident_Contract__c tc : trigger.new) {
        if (Agent.containsKey(tc.Seller_Agent_ID__c)) tc.Seller_Agent_Lookup__c = Agent.get(tc.Seller_Agent_ID__c).ID;
        tc.Seller_Agent_Office__c = Agent.get(tc.Seller_Agent_ID__c).Agent_Office__c;
    }
}

@isTest
private class TridentContract {
    static testmethod void testcontracts() {
        Trident_Contract__c contract1 = new Trident_Contract__c(Name = '1234568723423', Buyer_Agent_ID__c = '1234', Buyer_Agent_Branch__c = 'a02q0000002rils', Seller_Agent_ID__c = '1234', Seller_Agent_Branch__c = 'a02q0000002rils');

        test.startTest();

        insert contract1;


    }
}

Thank you for any suggestions. The trigger works in the Sandbox but it will not pass the test or deploy.

Best Answer

Here is the final code that worked

trigger AgentInfoSell on Trident_Contract__c (before insert, before update) {    
    Set<String> SellAGIDs = new Set<String>();
    for(Trident_Contract__c tc : trigger.new){
      if(tc.Seller_Agent_ID__c  !=NULL){SellAGIDs.add(tc.Seller_Agent_ID__c);

        Map <String, Agent__c> SellAG = new Map <String, Agent__c>();
        for (Agent__c obj: [SELECT ID, Agent_ID__c, Agent_Office__c
            from Agent__c
            Where Agent_ID__c IN: SellAGIDs]){SellAG.put(obj.Agent_ID__c,obj);}

        for(Trident_Contract__c t: trigger.new)
        {
            {
                if (SellAG.containsKey(tc.Seller_Agent_ID__c))  tc.Seller_Agent_Lookup__c=SellAG.get(tc.Seller_Agent_ID__c).ID;
                {
                    if (SellAG.containsKey(tc.Seller_Agent_ID__c)) tc.Seller_Agent_Office__c=SellAG.get(tc.Seller_Agent_ID__c).Agent_Office__c;}
                }