EntityCollectionBase.Count throws NullReferenceException

Posts   
 
    
Rahul
User
Posts: 3
Joined: 04-Aug-2011
# Posted on: 04-Aug-2011 23:24:57   

LLBLGen - v2.6 .NET framework - v3.5

Has anyone encountered a NullReferenceException in this situation ?


 public static CurrencyEntity GetCurrency(string currencyCode)
        {
            IPredicateExpression filter = new PredicateExpression(CurrencyFields.CurrencyCode == currencyCode);
            EntityCollectionBase<CurrencyEntity> entities = CurrencyFetch(filter, null);

            if (entities != null && entities.Count > 0) // throws NullReferenceException
            {
                return entities[0];
            }
            else
            {
                return null;
            }
        }


public static EntityCollectionBase<CurrencyEntity> CurrencyFetch(IPredicateExpression filter, ISortExpression sort)
        {
            EntityView<CurrencyEntity> jurisTypeView = new EntityView<CurrencyEntity>(GetCurrencies(), filter, sort);
            EntityCollectionBase<CurrencyEntity> entities = jurisTypeView.ToEntityCollection();
            return entities;
        }


 public static CurrencyCollection GetCurrencies()
        {
            if (currencies == null)
            {
                currencies = new CurrencyCollection();
                SortExpression sort = new SortExpression(CurrencyFields.CurrencyCode | SortOperator.Ascending);
                currencies.GetMulti(null, 0, sort);
            }

            return currencies;
        }


where CurrencyCollection : EntityCollectionBase<CurrencyEntity> and CurrencyEntity is an auto-generated Entity class

CallStack -

at SD.LLBLGen.Pro.ORMSupportClasses.EntityFieldCore.EntityFieldCoreInterpretGetValueInternal(IEntityCore entity) at SD.LLBLGen.Pro.ORMSupportClasses.FieldCompareValuePredicate.InterpretPredicate(IEntityCore entity) at SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression.InterpretPredicate(IEntityCore entity) at SD.LLBLGen.Pro.ORMSupportClasses.CollectionCore1.FindMatches(IPredicate filter) at SD.LLBLGen.Pro.ORMSupportClasses.EntityViewBase1.SetFilter(IPredicate filter) at SD.LLBLGen.Pro.ORMSupportClasses.EntityViewBase1.InitClassCore(CollectionCore1 relatedCollection, IPredicate filterToApply, ISortExpression sorterToApply, PostCollectionChangeAction dataChangeAction) at SD.LLBLGen.Pro.ORMSupportClasses.EntityView1..ctor(EntityCollectionBase1 relatedCollection, IPredicate filter, ISortExpression sorter, PostCollectionChangeAction dataChangeAction) at Avalara.AvaTax.Data.Util.CurrencyUtil.GetCurrency(String currencyCode) in d:\ccnet\deployment\avatax\avatax\branches\11.3\Data\Util\CurrencyUtil.cs:line 57 at Avalara.AvaTax.Services.Tax.Rules.CurrencyCodeRule.Apply(String currencyCode, String refersTo, CompanyEntity company, BaseResult result) at

[Deleted rest of the stack]

thanks, Rahul.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Aug-2011 00:01:15   

Hi Rahul,

Please post your LLBLGen version and Runtime Library version (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7725).

Also please post more relevant code. i.e, from the line you instantiate and fetch "entities" to the line you get the exception.

David Elizondo | LLBLGen Support Team
Rahul
User
Posts: 3
Joined: 04-Aug-2011
# Posted on: 09-Aug-2011 23:50:22   

Modified my original post and added code and version info.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Aug-2011 04:03:21   
public static EntityCollectionBase<CurrencyEntity> CurrencyFetch(IPredicateExpression filter, ISortExpression sort)
        {
            EntityView<CurrencyEntity> jurisTypeView = new EntityView<CurrencyEntity>(GetCurrencies(), filter, sort);
            EntityCollectionBase<CurrencyEntity> entities = jurisTypeView.ToEntityCollection();
            return entities;
        }

In above code you are passing the filter to a EntityView, which means that you are applying the filter in-memory and that is what the exception shows. Is that what you really want (filter in memory and not in database)? Please read that link to know what I'm talking about.

Due to the EntityView, a PostCollectionChangeAction is applied when you tried to access the Count (I guess). If you really want to filter in-memory and avoid that error, you could set the PostCollectionChangeAction.NoAction at the constructor:

EntityView<CurrencyEntity> jurisTypeView = new EntityView<CurrencyEntity>(GetCurrencies(), filter, sort, PostCollectionChangeAction.NoAction);

If you accidentally are using EntityView, just simply your code:

public static CurrencyEntity GetCurrency(string currencyCode)
{
    IPredicateExpression filter = new PredicateExpression(CurrencyFields.CurrencyCode == currencyCode);
    CurrencyCollection entities = GetCurrencies(filter);

    if (entities != null && entities.Count > 0) // throws NullReferenceException
    {
        return entities[0];
    }
    else
    {
        return null;
    }
}

public static CurrencyCollection GetCurrencies(IPredicateExpression filter)
{
    if (currencies == null)
    {
        currencies = new CurrencyCollection();
        SortExpression sort = new SortExpression(CurrencyFields.CurrencyCode | SortOperator.Ascending);
        currencies.GetMulti(filter, 0, sort);
    }

    return currencies;
}
David Elizondo | LLBLGen Support Team
Rahul
User
Posts: 3
Joined: 04-Aug-2011
# Posted on: 14-Aug-2011 06:02:56   

Great, thank you David !