locked
null reference exception while grouping contactstore contacts using linq by Email RRS feed

  • General discussion

  • In getData(StoredContact x) ,ss variable receives null when i directly run using F5...

    But When I go line by line using breakpoints then ss variable receives data what i want..

    What should i do sir??So that ss variable will receive correct data....

       async private void DefaultQuery()
            {

     ContactStore store = await ContactStore.CreateOrOpenAsync(
                    ContactStoreSystemAccessMode.ReadWrite,
                    ContactStoreApplicationAccessMode.ReadOnly);

                    ContactQueryResult KK = null;
                    KK= store.CreateContactQuery();

                    IReadOnlyList<StoredContact> contacts = null;
                    contacts  =await KK.GetContactsAsync();

     var Emailquery1 = contacts.GroupBy(x => ((((IDictionary<string, object>)getData(x) == null ? "" : ((IDictionary<string, object>)getData(x))["Email"]))))
                        .Where(g => g.Count() > 1)
                            .Select(y => new { Element = y.Key.ToString(), Counter = Convert.ToInt32(y.Count()) })
                             .ToList();

    }


    private IDictionary<string, object>  getData(StoredContact x)
            {

                IDictionary<string, object> ss = (IDictionary<string, object>)x.GetPropertiesAsync().GetResults();
                return ss;
            }

    this is method getData(StoredContact x)----




    Friday, September 4, 2015 11:19 AM

All replies

  • Please post a concise and complete example.

    Your code does not contain a GetData() method which assigns something to ss. Also there are some class declarations missing.

    But I looks like it's caused be your wired usage of await.


    Friday, September 4, 2015 11:26 AM
  • Sir.. getData() function i mentioned in code....Plz help me 


    Friday, September 4, 2015 11:57 AM
  • Nope. I don't find it.

    There is only a getData(StoredContact x).

    Friday, September 4, 2015 1:24 PM
  • You are right that is getData(StoredContact x) function.Actually Sir that is what I am talking about. 
    Monday, September 7, 2015 6:01 AM
  • Need to guess here:

    private IDictionary<string, object>  getData(StoredContact x)
    {
    	IDictionary<string, object> result = null;
    	var properites = (IDictionary<string, object>)x.GetPropertiesAsync().GetResults();
    	if (properites != null)
    	{
    		result = (IDictionary<string, object>)properites.GetResults();
    	}
    		
    	return result;
    } 
     
    -- OR
     
    private async IDictionary<string, object> getData(StoredContact x)
    {
    	IDictionary<string, object> result = await (IDictionary<string, object>)x.GetPropertiesAsync().GetResults();
    	return result;
    } 

    Monday, September 7, 2015 9:07 AM
  • thank you sir,But 1st function raised an error -----'System.Collections.Generic.IDictionary<string,object>' does not contain a definition for 'GetResults' and no extension method 'GetResults' accepting a first argument of type 'System.Collections.Generic.IDictionary<string,object>' could be found (are you missing a using directive or an assembly reference?)

    And 2nd function raised an error--------

    The return type of an async method must be void, Task or Task<T>

    Cannot await 'System.Collections.Generic.IDictionary<string,object>


    Monday, September 7, 2015 9:21 AM
  •    private IDictionary<string, object> getData(StoredContact x)
            {
                IDictionary<string, object> result = null;
                var properites = (IDictionary<string, object>)x.GetPropertiesAsync().GetResults();
                if (properites != null)
                {
                    result = (IDictionary<string, object>)properites;
                }

                return result;
            } 

    I edited 1st code..Then doesnt show any error.... Now Im Having 2 entries

    a>Name:parag,Email:p

    a>Name:Kalpesh,Email:p

    the problem is when I run using breakpoint and go line by line using F10 to check values

    Emailquery1 variable has values like  [0] {Element="p",Counter=2} this is good....

    But when I directly run it using F5 whether there are breakpoints or not 

    Emailquery1 variable has values like  [0] {Element=" ",Counter=2} this is bad result.In this case result variable receives null values

    finally...In function we have these statements

       IDictionary<string, object> result = null;
                var properites = (IDictionary<string, object>)x.GetPropertiesAsync().GetResults();
                if (properites != null)
                {
                    result = (IDictionary<string, object>)properites;
                }

         return result;

    if i put breakpoint on first statement and return statement.when i run it and go line by line using F10 then result variable receives correct data but If I run it using F5 then result variable receives null value.

    Sir I think something getting missed.Im not getting understand what the actual problem is?
    Monday, September 7, 2015 9:52 AM
  • private IDictionary<string, object>  getData(StoredContact x)
    {
    	IDictionary<string, object> result = null;
    	var properites = x.GetPropertiesAsync();
    	if (properites != null)
    	{
    		result = (IDictionary<string, object>)properites.GetResults();
    	}
    		
    	return result;
    } 
     

    The first should be

    Monday, September 7, 2015 11:58 AM
  • Check the signature of GetPropertiesAsync().. does it have an async modifier. Then you need the await call.
    Monday, September 7, 2015 11:59 AM
  • private IDictionary<string, object> getData(StoredContact x) { IDictionary<string, object> result = null; var properites = x.GetPropertiesAsync(); if (properites != null) { result = (IDictionary<string, object>)properites.GetResults(); } return result; } This is ok if i go line by line using breakpoints but when I run it directly raises Object reference error..

    It means result variable receives null.Now GetPropertiesAsync() has async modifier So code is made like

       var Emailquery1 = contacts.GroupBy(x=>Convert.ToString(getData(x).Result["Email"]))
                        .Where(g => g.Count() > 1)
                            .Select(y => new { Element = y.Key.ToString(), Counter = Convert.ToInt32(y.Count()) })
                             .ToList();

      private async Task<IDictionary<string, object>> getData(StoredContact x)
            {
                IDictionary<string, object> result = null;
                IDictionary<string, object> properites = await x.GetPropertiesAsync();
                if (properites != null)
                    result = (IDictionary<string, object>)properites;

                return result;
            } 

    Here, GetPropertiesAsync() stops execution Suddenly....

    I think sir...The Problem i have been facing due to Async method GetPropertiesAsync()

    Is there any solution about that sir?

    OR Is there other way to access contactstore properties like Email,PhoneNumbers for grouping?






    Tuesday, September 8, 2015 9:55 AM
  •    private IDictionary<string, object> getData(StoredContact x = null)
            {
                IDictionary<string, object> result = null;
                //Task.Delay(100);
                Task<IDictionary<string, object>> properites = x.GetPropertiesAsync().AsTask();
                //properites.Wait();
                if (properites != null)
                    result = (IDictionary<string, object>)properites.Result;
                return result;
            }


    Thank You sir for helping and supporting .....I got Solution 

     Task<IDictionary<string, object>> properites = x.GetPropertiesAsync().AsTask();  

    Will you please provide your EmailID for asking Queries In Future?
    Tuesday, September 8, 2015 11:13 AM
  • Now,Question-how to solve Error?

    Error:Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

    var Emailquery1 = contacts.GroupBy(x =>new{ getData(x,1)["Email"].ToString(),getData(x,2)["MobileTelephone"].ToString()})
                        .Where(g => g.Count() > 1)
                            .Select(y => new { Element = y.Key.ToString(), Counter = Convert.ToInt32(y.Count()) })
                             .ToList();

        private IDictionary<string, object> getData(StoredContact x, int Prop)
            {
                IDictionary<string, object> result = null;
                //Task.Delay(100);
                Task<IDictionary<string, object>> properites = x.GetPropertiesAsync().AsTask();
                //properites.Wait();
                if (properites != null)
                    result = (IDictionary<string, object>)properites.Result;

                if (!result.ContainsKey("Email") && Prop == 1)
                    result.Add("Email", "");
                if (!result.ContainsKey("MobileTelephone") && Prop == 2)
                    result.Add("MobileTelephone", "");
                return result;
            }


    Wednesday, September 9, 2015 12:46 PM
  • Open a new question, please.
    Wednesday, September 9, 2015 12:52 PM