object reference not set to an instance

Posts   
 
    
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 11-Mar-2005 09:58:07   

I'm doing the following:

PageEntity page = PageManager.FetchWithScope(pageId); if(page.Scope.Fields.State == EntityState.Fetched) return page.Scope.Text; return string.Empty;

I get the above error message, " object reference........". Now I know that for this given pageId the ScopeEntity IS Not created in the db, but that's what I thought I was checking for. I would like to test for a whether scope is valid or not. Just to mention, the page entity is valid.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Mar-2005 10:24:56   

No the entity reference is null, page.Scope is null. (you're using adapter am I correct?) as it isn't fetched.

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 11-Mar-2005 10:40:47   

Correct, I'm using adapter. Correct, the reference is null, not sure how to test for that. Yes, then entity is not fetched because the entity is not in the db which I believe is my problem. So I don't think this is the correct way to do this.

[Edit] Ok, simple solution, just add a check to see if page.Scope is null which worked.

PageEntity page = PageManager.FetchWithScope(pageId); if(page.Scope == null) return string.Empty; else { // if(page.Scope.Fields.State == EntityState.Fetched) return page.Scope.Text; }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Mar-2005 10:59:16   

erichar11 wrote:

Correct, I'm using adapter. Correct, the reference is null, not sure how to test for that. Yes, then entity is not fetched because the entity is not in the db which I believe is my problem. So I don't think this is the correct way to do this.

do:

PageEntity page = PageManager.FetchWithScope(pageId); if((page.Scope==null)||(page.Scope.Fields.State != EntityState.Fetched)) { return string.Empty; } return page.Scope.Text;

the first clause is then already true and .NET stops evaluating the whole if clause, as the rest won't change the outcome of the expression.

(edit) too late... simple_smile

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 11-Mar-2005 11:09:23   

thanks, someone wrote in a thread here about placing a problem on this forum and right after he submitted it to the forum, he realized what the solution to his problem was. Geez, how true is that. smile This forum is magic.

Now only if I can get subversion to commit my changes rage , I'd go to sleep happy.

[Edit] Finally got subversion to commit my changes, but it took a couple of hours to figure out why. My fault actually as due to the issue I had with llblgen regarding an entity not showing up correctly in the designer, I deleted some directories so I could regenerate fresh. Well those directories had _svn directories which were also deleted. The root of the problem. Fixed and all is well.