locked
list reference becoming null RRS feed

  • Question

  • hi guys,

    getting null reference exception in postback even though List object is initialized in pageload

     

     ServerList myserverlist;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                   BindGrid();
                }
            }

    myserverlist is null in postback

    why?

     


    regards, Satish
    • Moved by Andreas Johansson Tuesday, August 16, 2011 12:25 PM ASP.NET (From:Visual C# General)
    Tuesday, August 16, 2011 11:38 AM

All replies

  • I got the answer by making list static

     

    Can anyone tell the reason? still figuring it out!


    regards, Satish
    Tuesday, August 16, 2011 11:47 AM
  • The reason is obvious. Once the page is rendered, the list becomes null. When the request is posted back, the list is not there !!!
    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, August 16, 2011 11:59 AM
  • Please ask ASP.NET related questions in the ASP.NET forums.

    http://forums.asp.net

     

    Tuesday, August 16, 2011 12:24 PM
  • but how static solved the problem
    regards, Satish
    Tuesday, August 16, 2011 12:42 PM
  • this is my first Q in forums so will take care next time

     

    thanks for your suggestion


    regards, Satish
    Tuesday, August 16, 2011 12:43 PM
  • Don't worry, if you have non-ASP.NET questions even if it is for web application this is still the place to post but when related to ASP.NET it you get better and faster help in those forums

    The answer to your question is related to the page lifecycle in ASP.NET.

    http://msdn.microsoft.com/en-us/library/ms178472.aspx

    Making it static make it live outside the page lifecycle and there is only one list in your appliaction, it will be the same for all different users (whch may be what you want) . However this isn't the place for ASP.NET discussion ... :)

    Tuesday, August 16, 2011 1:46 PM