简体   繁体   中英

Object throws NullReferenceException

  • I have a class Product containing few public properties

  • I have another class ListOfProducts that should contain a List of Product objects

  • I have a method in my service.svn class where I'm retrieving rows and want to add Product objects into the List present in class ListOfProducts by creating an object of ListOfProducts and return this object. But seems like Its not the way it should be done. Because the service_GetObjectCompleted that receives this List throws NullReferenceException .

ListOfProducts class

[DataContract()]
public class ListOfProducts
{
    [DataMember()]
    public List<Product> ProductList { get; set; }

    public ListOfProducts()
    {
        ProductList = new List<Product>();
    }
}

The method in Service.svn class that creates an object ListOfProducts and add Product objects to its List

public ListOfProducts GetObject()
{
    ListOfProducts Listproducts = new ListOfProducts();
    ........
    using (IDataReader reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            Product product = new Product(reader["Name"].ToString(), reader["Code"].ToString());
            Listproducts.ProductList.Add(product);
        }
    }
    return Listproducts;
}

WCF's Completed Event which receives Listproducts in e returned from the above method:

void service_GetObjectCompleted(object sender, GetObjectCompletedEventArgs e)
{
    if (e.Result.Count != 0)  //throws NullReferenceException
    {
        PagedCollectionView pagingCollection = new PagedCollectionView(e.Result);
        pgrProductGrids.Source = pagingCollection;
        grdProductGrid.ItemsSource = pagingCollection;
    }
}

I think my concept is wrong over here. Is it the right way to create Object of List?

EDIT

In the Page's Constructor, this is how I am subscribing the GetObjectCompleted event

service.GetObjectCompleted += service_GetObjectCompleted;

On a button click event I am calling GetObject Asynchronously

service.GetObjectAsync();

The deserializer did not call your constructor!

Therefore, when you retrieve your ListOfProducts on the other end of the service, the ProductList property is still null .

SOLVED

The problem was in the service_GetObjectCompleted event. Instead of referencing the list like e.Result , I needed to reference it like e.Result.ProductList . So here is the version that works:

void service_GetObjectCompleted(object sender, GetObjectCompletedEventArgs e)
{
    if (e.Result.Productlist.Count != 0)  
    {
        PagedCollectionView pagingCollection = new PagedCollectionView(e.Result.Productlist);
        pgrProductGrids.Source = pagingCollection;
        grdProductGrid.ItemsSource = pagingCollection;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM