[SalesForce] Error Variable does not exist

I am new at Apex and I ran into an error. Here's my code, the line with the error is at the end.

TOT_List_fromQuery = [
    SELECT Id, Owner.FirstName, Owner.LastName, Comments_esi__c, Date_vod__c,
    Time_vod__c, CreatedBy.Email, OwnerId, Name, EP_Territory__c, ONC_Territory__c,
    Reason_vod__c, Status_vod__c, RecordTypeId, End_date_esi__c, Reminder_Days_esi__c
    FROM Time_Off_Territory_vod__c 
    WHERE Id IN :totIds
    AND (Reason_vod__c = 'hhc Activity' OR Reason_vod__c = 'Paid Time Off')
    AND (Status_vod__c = 'Approved' OR Status_vod__c = 'Rejected'
         OR Status_vod__c = 'Requested')];

if (TOT_List_fromQuery != null && TOT_List_fromQuery.size() > 0)
{
    for (Time_Off_Territory_vod__c toTObj : TOT_List_fromQuery)
    {
        ownerIds.add(toTobj.OwnerId);
    }
    ownerTerrs = [SELECT UserId, TerritoryId FROM UserTerritory
                  WHERE UserId IN :ownerIds];

    OwnerIdLocaleMap = mapOwnerLocale(ownerIds);

    for (UserTerritory ut : ownerTerrs)
    {
        // Creating owner id, territory Id map
        ownerTerrIdMap.put(ut.UserId, ut.TerritoryId);
    } 

    for (Time_Off_Territory_vod__c toTObj : TOT_List_fromQuery)
    {
        System.debug('---1---Inside for tot record---' + toTobj);
        // Enters if Approver email Id is null
        Id terrId = ownerTerrIdMap.get(toTObj.OwnerId);
        Integer mail_sent = 0;
        // toTObj.RecordTypeId = RecordtypeObj.Id;
        while (terrParentTerrMap.containsKey(terrId))
        {
            System.debug('---2---inside while');
            Id parentTerrId = terrParentTerrMap.get(terrId);
            System.debug('terrId:--' + terrId);
            System.debug('ParentTerrId:--' + parentTerrId);
            if (parentIdUserEmailMap.containsKey(parentTerrId))
            {
                System.debug('useremail map contains key' + parentIdUserEmailMap.containsKey(parentTerrId));
                System.debug('$$$$$$$emailList' + parentIdUserEmailMap.get(parentTerrId));
                String toTURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + toTObj.Id;
                String comment = '';

                // Commenting the following code to have date format in specific to locale of user
                /*Date dT = toTObj.Date_vod__c;
                String RequestedDate = dT.month() + '/' + dT.day() + '/' + dt.year();
                Date endDate = toTObj.End_date_esi__c;
                String totEndDate = endDate.month() + '/' + endDate.day() + '/' + endDate.year();*/

                String RequestedDate; 
                String totEndDate;

                //String RequestedDate; = dateUtility(toTObj.Date_vod__c, toTObj.ownerId);
                //String totEndDate; = dateUtility(toTObj.End_date_esi__c, toTObj.ownerId);

                if (OwnerIdLocaleMap.get(toTObj.ownerId) == 'es_MX')
                {
                    RequestedDate = toTObj.Date_vod__c.day() + '/' + toTObj.Date_vod__c.month() + '/' + toTObj.Date_vod__c.year();
                    totEndDate = toTObj.End_date_esi__c.day() + '/' + toTObj.End_date_esi__c.month() + '/' + toTObj.End_date_esi__c.year();
                }
                else
                {
                    RequestedDate = toTObj.Date_vod__c.month() + '/' + toTObj.Date_vod__c.day() + '/' + toTObj.Date_vod__c.year();
                    totEndDate = toTObj.End_date_esi__c.month() + '/' + toTObj.End_date_esi__c.day() + '/' + toTObj.End_date_esi__c.year();
                }

                if (toTObj.Comments_esi__c != null && toTObj.Comments_esi__c != '')
                    comment = toTObj.Comments_esi__c;

                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                if (sendCcMail == true && toTObj.Reason_vod__c != 'hhc Activity')
                {
                    //mail.setCcAddresses(new String[]{ System.Label.Eisai_Msl_cc_email });

                    for (Time_Off_Territory_vod__c ccLisTtot : TOT_List_fromQuery)
                    {
                        // >>> THIS IS THE LINE WITH ERROR
                        if ((IsValidCcemailId(oncMslCcAddressList, parentIdUserEmailMap.get(parentTerrId)) == false) && (ONC_Territory__c != null))
                            mail.setCcAddresses(oncMslCcAddressList);
                        if ((IsValidCcemailId(epMslCcAddressList, parentIdUserEmailMap.get(parentTerrId)) == false) && (EP_Territory__c != null))
                            mail.setCcAddresses(epMslCcAddressList);
                    }
                }
            }
        }
    }
}

Best Answer

The error is at EP_Territory__c!=null, because no variable is defined that has that name. You probably meant toTObj.EP_Territory__c!=null, which would reference an SObject (toTObj) and a field within that SObject (EP_Territory__c).