Community
Showing results for 
Search instead for 
Do you mean 
Reply

Error: "Object reference not set to an instance of an object" when retrieving contacts based on email (C#)

Accepted Solution Solved
Copper Super Contributor
Posts: 28
Country: Canada
Accepted Solution

Error: "Object reference not set to an instance of an object" when retrieving contacts based on email (C#)

ISSUE:

I'm having trouble receiving certain data back when looking up contacts by email.

 

CONTEXT

To get contacts by email, to find duplicates. These duplicates will be managed after they are retrieved

 

 

I'm using this method to get contacts by email like so:

public ContactList getContactByEmail(string email)
{
    ACTFM.LogOn((path), user, pass);

    //declare
    CriteriaColumn cColumn;
    OperatorEnum oOperator;
    string sValue;
    ContactLookup cLookup;
    ContactList cList;

    //init
    oOperator = OperatorEnum.EqualTo;
    sValue = email;

    cColumn = ACTFM.Lookups.GetCriteriaColumn("TBL_CONTACT", "BUSINESS_EMAIL", true);

    //hold lookup in array USE THE ***END*** method
    Criteria[] lCriteria = new Criteria[] { 
            new Criteria(LogicalOperator.End, (byte)0, (byte)0,    cColumn, oOperator, email)
    };

    //create lookup
    cLookup = ACTFM.Lookups.LookupContactsReplace(lCriteria, true, true);

    cList = cLookup.GetContacts(null);

    ACTFM.LogOff();
    return cList;
}


 

 

And inside another method I make the call to the ContactList method like so:

ContactList similarsList = act.getContactByEmail(row.Cells[3].Text);

foreach (Act.Framework.Contacts.Contact con in similarsList)
{
    Debug.WriteLine("a contact: " + con.FirstName);
}

 

When doing so at line:

Debug.WriteLine("a contact: " + con.FirstName);

I get the error: "Object reference not set to an instance of an object".

 

----------------------------------------------------------------------------------------------------------------------------------------------------------

 

Interestingly enough when I use "con.ID" like so:

Debug.WriteLine("a contact: " + con.ID);

Instead of:

Debug.WriteLine("a contact: " + con.FirstName);

I get the expected ID's of the contacts I am looking up. So I know the code is working to some extent?

 

QUESTION:

What could be causing the code to fail when trying to access the contact fields, but work when accessing the ID field?

 


Accepted Solutions
Solution
Accepted by topic author 2gen
‎09-25-2015 03:20 AM
Copper Super Contributor
Posts: 28
Country: Canada

Re: Error: "Object reference not set to an instance of an object" when retrieving contacts based on email (C#)

[ Edited ]

Yeah I tested to see what was going on. The weird thing was that I have access to the ACT CRM and I could see the contacts, and that they did indeed have the values inside that I was trying to retrieve.

 

I've since solved the issue, though. It turns out that doing:

ACTFM.Logoff();

before accessing and utilzing the properties of con, wiped out the memory that the instance of ContactList was referenced to. I'm assuming this is a security feature, so that even after legitimately accessing the data, you cannot access it unless logged in. Sigh.

 

 

Thanks for your reply though, had I still been in the dark, it would have been a breath of fresh air!

View solution in original post


All Replies
Copper Super Contributor
Posts: 52
Country: United_Kingdom

Re: Error: "Object reference not set to an instance of an object" when retrieving contacts based on email (C#)

Hi 2gen,

 

Have you done any work to try and find out if any of your objects are null?

 

You should check cColumn, cLookup and cList. Also check how many contact exist in cList using

 

MessageBox.Show(cList.Count.ToString());

 If this doesn't point you to the answer please just let me know and I'l try and assist further.

 

Regards,

 

 

Russell

 

Solution
Accepted by topic author 2gen
‎09-25-2015 03:20 AM
Copper Super Contributor
Posts: 28
Country: Canada

Re: Error: "Object reference not set to an instance of an object" when retrieving contacts based on email (C#)

[ Edited ]

Yeah I tested to see what was going on. The weird thing was that I have access to the ACT CRM and I could see the contacts, and that they did indeed have the values inside that I was trying to retrieve.

 

I've since solved the issue, though. It turns out that doing:

ACTFM.Logoff();

before accessing and utilzing the properties of con, wiped out the memory that the instance of ContactList was referenced to. I'm assuming this is a security feature, so that even after legitimately accessing the data, you cannot access it unless logged in. Sigh.

 

 

Thanks for your reply though, had I still been in the dark, it would have been a breath of fresh air!