views:

1481

answers:

6

I've a user control which contains asp:Literal.

<div>
     <asp:Literal id="MenuContainer" runat="server" />
</div>

There is a method in code-behind page which initializes the control:

internal void Setup(MyBusinessObject obj)
{
    MenuObject menu = MenuHelper.GetMenu(obj.State);

    if(obj == null)
        MenuContainer.Visible = false;

    //other code
}

In the page where the control is used I call Setup method of control in LoadComplete event handler (I was first calling it in Load event). Irrespective of MyBusinessObject being null or not null, when I access Literal on user-control I get error:

Object reference not set to an instance of an object.

What is the reason and what is the remedy for this?

A: 

Are you sure MenuContainer is the problem? You are referencing obj.State in the first line of the Setup function. If that obj is null you'll get that error.

JerSchneid
Yes, It is the problem. if(obj == null) passes. MenuContainer.Visible throws exception.
TheVillageIdiot
A: 

As mentioned in the answer by JerSchneid, if obj is null, you'll get that error. So, try doing it like this -

internal void Setup(MyBusinessObject obj)
{
    if(obj == null)
        MenuContainer.Visible = false;
    else
        MenuObject menu = MenuHelper.GetMenu(obj.State);
}

EDIT: I know you are getting an error on that line, but just try doing it like this. Or, else, remove the whole code and just keep the MenuContainer.Visible = false; line.

Kirtan
Dear the obj is not null only the MenuContainer is null. That is why I'm puzzled.
TheVillageIdiot
A: 

If MenuContainer is null, it probably has something to do with the time line of the page life cycle. You're calling that function before MenuContainer is linked up. Can you try calling Setup in the Page_Load function?

JerSchneid
A: 

The code you posted is the following:

internal void Setup(MyBusinessObject obj)
{    
    MenuObject menu = MenuHelper.GetMenu(obj.State);    

    if(obj == null)        
        MenuContainer.Visible = false;    //other code
}
  • If obj is null, then dereferencing obj.State on the first line will throw a NullReferenceException

  • If obj is not null, the line MenuContainer.Visible = false won't be executed.

So I don't think you're posting all the relevant code.

When you're having difficulty debugging this kind of thing, try stepping through the code with the debugger or adding some asserts to your code, which will help you to see exactly what's happening:

internal void Setup(MyBusinessObject obj)
{    
    Debug.Assert(obj != null);
    MenuObject menu = MenuHelper.GetMenu(obj.State);    

    Debug.Assert(MenuContainer != null);
    if(obj == null)        
        MenuContainer.Visible = false;    //other code
}
Joe
+3  A: 

It was very simple. I was adding things in web.config's controls section as was suggested by Rick Sthral in one of his post ( :( for got about the post, you will have to search on his page).

It was nicely allowing me to add controls without putting @ Register tag but the downside was that child controls on my controls were shown as null! so I simply put @ Register directive in my pages and it worked.

TheVillageIdiot
+1, thanks! Though can anyone explain this nonsensical solution? Isn't the web.config shortcut supposed to remove the need for littering the page markup with Register directives? Why even bother if we have to put it there anyway to prevent strange, hair-pulling-out errors like this? Okay, </rant>.
Yadyn
@Yadyn that's what I'm not able to understand. I've put comment on Rick's blog but haven't got any reply.
TheVillageIdiot
+1  A: 

Thanks, TheVillageIdiot, for posting the answer to your problem - I ran into exactly the same misunderstanding.

Adding controls via in the web.config was not enough to actually use it! I tried it like this on a page: ... but this would lead to the phenomenon, that none of the controls inside the UserControl were initialized. Only adding <%@ Register Src="~/UserControlsAccount/LoginMessages.ascx" TagPrefix="user" TagName="Messages" %> to the top of the page solved the problem :-)

Thanks again!