Object reference not set to an instance of an object with an Entity

Posts   
 
    
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 07-Jan-2010 11:50:28   

Hello,

I've got the following method:


        /// <summary>
        /// Insert or Update an address.
        /// </summary>
        /// <param name="u4Address"></param>
        /// <returns></returns>
        private AddressEntity InsertOrUpdateAddress(getdebtors_ttAddressRow u4Address)
        {
            try
            {
                AddressEntity addressEntity = new AddressEntity(u4Address.AddressCode)
                {
                    AddressCode = u4Address.AddressCode,
                    AddressDescr = u4Address.AddressDescr,
                    AddressType = u4Address.AddressType,
                    CountryName = u4Address.CountryName,
                    DebtorCode = u4Address.DebtorCode,
                    EanCode = u4Address.EanCode,
                    Place = u4Address.Place,
                    Priority = u4Address.Priority,
                    SearchKey = u4Address.SearchKey,
                    Street = u4Address.Street,
                    Zipcode = u4Address.Zipcode
                };

                //Insert of Update the address. If no data is changed (IsDirty = false) no update query is executed.
                addressEntity.Save();

                return addressEntity;
            }
            catch (Exception ex)
            {
                DigibizException.LogException(ex);
                return null;
            }
        }

This method is being executed in a foreach loop:


                foreach (getdebtors_ttDebtorRow u4Debtor in totalDebtors)
                {
                    //Insert or Update the debtor.
                    InsertOrUpdateDebtor(u4Debtor);
                }

totalDebtors contains 10263 rows. The code runs perfect for the first 4943 rows, but after this I get the following exception:

Exception: Object reference not set to an instance of an object. - StackTrace: at Eurofysica.BusinessLogic.BLL.Controllers.MemberController.InsertOrUpdateAddress(getdebtors_ttAddressRow u4Address) in C:\SVN\Eurofysica\Eurofysica.UmbracoExtension\Eurofysica.BusinessLogic\BLL\Controllers\MemberController.cs:line 185

Does anybody know how this is possible?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Jan-2010 12:29:51   

Exception: Object reference not set to an instance of an object. - StackTrace: at Eurofysica.BusinessLogic.BLL.Controllers.MemberController.InsertOrUpdateAddress(getdebtors_ttAddressRow u4Address) in C:\SVN\Eurofysica\Eurofysica.UmbracoExtension\Eurofysica.BusinessLogic\BLL\Controllers\MemberController.cs:line 185

Which line of code is the 185th?

What value do you have for LazyLoadingWithoutResultReturnsNew in the project properties?

Also would you please add the following check:

        private AddressEntity InsertOrUpdateAddress(getdebtors_ttAddressRow u4Address)
        {
            if(u4Address == null)
                return new AddressEntity(); //or null       

            ...
        }
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 07-Jan-2010 13:04:24   

Walaa wrote:

Also would you please add the following check:

        private AddressEntity InsertOrUpdateAddress(getdebtors_ttAddressRow u4Address)
        {
            if(u4Address == null)
                return new AddressEntity(); //or null       

            ...
        }

Stupid I didn't try this before. Somehow some of the objects in the collection are null. Now everything works again simple_smile . Thanks!