Where's my problem?

Posts   
 
    
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 22-Apr-2005 22:47:25   

Help. I'm getting an "Object reference not set an instance of an object" error when doing a FetchEntityCollection in adapter. Here's my code. Where did I blow it?

EntityCollection cal = new EntityCollection(new CalendarEntityFactory());

IRelationPredicateBucket calF = new RelationPredicateBucket();
calF.Relations.Add(CalendarEntity.Relations.SpecialDayEntityUsingCalDate);
calF.PredicateExpression.Add(PredicateFactory.Between(CalendarFieldIndex.CalDate,startDate,endDate));

calF.PredicateExpression.AddWithAnd(PredicateFactory.CompareValue(SpecialDayFieldIndex.LocId, ComparisonOperator.Equal,542));
calF.Relations.Add(SpecialDayEntity.Relations.CdSpecialDayEntityUsingCode);
calF.PredicateExpression.AddWithAnd(PredicateFactory.CompareValue( CdSpecialDayFieldIndex.IsInstructionDay,ComparisonOperator.Equal,0));
            
IPrefetchPath2 spcP = new PrefetchPath2((int)EntityType.SpecialDayEntity);
spcP.Add(SpecialDayEntity.PrefetchPathCdSpecialDay);
            
ISortExpression calS = new SortExpression(SortClauseFactory.Create(CalendarFieldIndex.CalDate,SortOperator.Ascending));

using (DataAccessAdapter da = new DataAccessAdapter())
{
    da.FetchEntityCollection(cal,calF,0,calS,spcP);
}

...and here's the stack trace

[NullReferenceException: Object reference not set to an instance of an object.]
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.MergeNormal(IEntityCollection2 rootEntities, IPrefetchPathElement2 currentElement, Boolean rootEntitiesArePkSide) in c:\Myprojects\VS.NET Projects\LLBLGen Pro\RuntimeLibraries 1.0.2004.2\ORMSupportClasses\DataAccessAdapterBase.cs:3594
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchPrefetchPath(IEntityCollection2 rootEntities, IRelationPredicateBucket filterBucket, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath) in c:\Myprojects\VS.NET Projects\LLBLGen Pro\RuntimeLibraries 1.0.2004.2\ORMSupportClasses\DataAccessAdapterBase.cs:3539
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollection(IEntityCollection2 collectionToFill, IRelationPredicateBucket filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath) in c:\Myprojects\VS.NET Projects\LLBLGen Pro\RuntimeLibraries 1.0.2004.2\ORMSupportClasses\DataAccessAdapterBase.cs:1848
   HLPUSD.SMART.BLL.StudentManager.GetReleaseDayList(String stuID, DateTime startDate, DateTime endDate) in g:\documents and settings\njdevore\my documents\visual studio projects\smartbll\studentmanager.cs:303
   HLPUSD.SMART.ParentPortal.stuHome.fetchReleaseList(StudentManager stuMgr) in g:\inetpub\wwwroot\smartparentportal\stuhome.aspx.cs:107
   HLPUSD.SMART.ParentPortal.stuHome.fetchStatistics(StudentManager stuMgr) in g:\inetpub\wwwroot\smartparentportal\stuhome.aspx.cs:71
   HLPUSD.SMART.ParentPortal.stuHome.Page_Load(Object sender, EventArgs e) in g:\inetpub\wwwroot\smartparentportal\stuhome.aspx.cs:38
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()

I have my Calendar table which holds one record for every day of the school year. The SpecialDay table holds all the holidays for each LocID. So Calendar is joined to SpecialDay via CalDate and SpecialDay is joined to the code set CdSpecialDay so that I can know if it is a holiday or a school release day. Anyway, what am I missing?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 22-Apr-2005 23:34:27   

Are you using the latest beta? Or an old beta? Could you re-try with the final bits just released?

I'll look into your code tomorrow to see if the error is in there or the error is in my code.

Frans Bouma | Lead developer LLBLGen Pro
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 23-Apr-2005 00:24:23   

Otis wrote:

Are you using the latest beta? Or an old beta? Could you re-try with the final bits just released?

I'll look into your code tomorrow to see if the error is in there or the error is in my code.

I downloaded the release today and still got the error. But I'm not suprised because I'm still learning how to do even simple things with your code. So, let me write the sql how I want it to look and you can point out where I went wrong.

select cal.*, spc.code, cds.abbr
from calendar as cal, special_day as spc, cd_special_day as cds
inner join cal.cal_date on spc.cal_date
inner join cds.code on spc.code
where cal.cal_date between '6/14/2004' and '6/30/2005'
and cds.is_instructional_day = 0

I understand that it will be done with graphs and that is fine, but you understand what I'm after at least.

Secondly, once you help me get this working, how do I use elements from the nested entities? For each CalendarEntity in my EntityCollection, I can get at it by just referencing the column name I want (like CalDate). But how do I get at the Abbr field inside of CdSpecialDay which is an Entity of the SpecialDay entity which is an entity of each CalendarEntity inside my collection?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 23-Apr-2005 11:31:36   

The crash is at the line which grabs a field from a relation object. It seems that's not correctly constructed. Very weird. I'll try to reproduce your query with a similar construct and see where it breaks. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 23-Apr-2005 13:33:18   

You're after a list of fields from 3 tables, though you're fetching entity objects. If you want a list, create a typed list and fill that one. if you want a set of objects in a hierarchy, use a prefetch path and fetch the objects in a collection. So your query which crashes isn't what you need, you need a typed list.

BUT, if you want to work with entities later on, you have to fetch them in a graph. Though the sql you showed just shows a list of fields from 3 tables joined together.

I'm still curious why the code crashes, so I still will check it out.

Frans Bouma | Lead developer LLBLGen Pro
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 23-Apr-2005 20:24:07   

Otis wrote:

You're after a list of fields from 3 tables, though you're fetching entity objects. If you want a list, create a typed list and fill that one. if you want a set of objects in a hierarchy, use a prefetch path and fetch the objects in a collection. So your query which crashes isn't what you need, you need a typed list.

I will create a typed list then because you're right, all I really care about is displaying the data, not working with the entities later on.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 24-Apr-2005 11:08:15   

I saw the error! smile . The clue was that the exception occured in the routine where the FK field was retrieved from the fetched entity based on the name used in the relation and that field wasn't found. So the FK entities had to be of a different type.

Well, they are simple_smile . You fetch an entity collection of calender entites, though you start your prefetch path somewhere else:


IPrefetchPath2 spcP = new PrefetchPath2((int)EntityType.SpecialDayEntity);

That's not correct, you have to specify the root entity type of the entity you're fetching with the method, in this case CalenderEntity:


IPrefetchPath2 spcP = new PrefetchPath2((int)EntityType.CalenderEntity);

The rest of the code should be the same. The path defines the graph from the root entity, including the root entity/entities, which is the entity /entities you're fetching with the method you're calling, in this case CalenderEntity, so the path has to start there. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
NickD
User
Posts: 224
Joined: 31-Jan-2005
# Posted on: 25-Apr-2005 16:23:34   

Otis wrote:

The rest of the code should be the same. The path defines the graph from the root entity, including the root entity/entities, which is the entity /entities you're fetching with the method you're calling, in this case CalenderEntity, so the path has to start there. simple_smile

Thanks for showing me the errors of my ways.