0

I am Trying to access one of my page say staffs.aspx and getting the following error/ exception.

Error Message: Object reference not set to an instance of an object.

Stack Trace: at BasePage.LoadPageStateFromPersistenceMedium() in c:\TeamCity\buildAgent\work\895f95277d6888cc\FrontEnd\App_Code\BasePage.cs:line 17 at System.Web.UI.Page.LoadAllState() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Though, it runs fine on my system but my client is facing problem to access this page.

Here Is The piece of code ...

  protected override object LoadPageStateFromPersistenceMedium()
    {
        System.Web.UI.PageStatePersister pageStatePersister = this.PageStatePersister;
        pageStatePersister.Load();
byte[] bytes = Convert.FromBase64String(pageStatePersister.ViewState.ToString());//Line 17
        bytes = Common.Decompress(bytes);
        LosFormatter losFormatter = new LosFormatter();
        Object viewState = losFormatter.Deserialize(Convert.ToBase64String(bytes));
        return new Pair(pageStatePersister.ControlState, viewState);
    }

here is the code for SavePageStateToPersistenceMedium :

  protected override void SavePageStateToPersistenceMedium(object state)
    {
        Pair pair;
        System.Web.UI.PageStatePersister pageStatePersister = this.PageStatePersister;
        Object viewState;
        if (state is Pair)
        {
            pair = ((Pair)state);
            pageStatePersister.ControlState = pair.First;
            viewState = pair.Second;
        }
        else
            viewState = state;

        LosFormatter losFormatter = new LosFormatter();
        StringWriter stringWriter = new StringWriter();
        losFormatter.Serialize(stringWriter, viewState);
        byte[] bytes = Convert.FromBase64String(stringWriter.ToString());
        bytes = Common.Compress(bytes);
        pageStatePersister.ViewState = Convert.ToBase64String(bytes);
        pageStatePersister.Save();
    }

FYI : I thought may be its a browser issue so i tried this page in multiple browsers , and all worked fine for me. worked with cache clear , database calls and redirections to the suspected page. but got no error. So I am unable to understand what is producing a null object.

Thanks!

  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – M. Wiśnicki Jan 02 '17 at 13:35
  • What is on line `17` of `BasePage.cs`? – VDWWD Jan 02 '17 at 13:48
  • Please paste the code chunk surrounding the line number 17. Null reference will occur when you try to get value from a null object. – Dinesh Prajapati Jan 02 '17 at 15:35
  • @M.Wiśnicki I am aware of what is nullreference exception but in my case i am unable to find what is producing a null value. moreover its a viewstate error and in stack trace i am unable to find it. – Umara A. Zahid Jan 03 '17 at 05:15
  • @DineshPrajapati I have edited my question and pasted my piece of code too. Thanks! – Umara A. Zahid Jan 03 '17 at 05:23
  • @VDWWD I have edited my code. Any Help Is Appreciable.Thanks – Umara A. Zahid Jan 03 '17 at 05:24
  • @UmaraA.Zahid Is the `ViewState` enabled on the page where you are getting this exception? – Siva Gopal Jan 03 '17 at 05:41
  • @SivaGopal yes its enabled...therefore i am having issue with one client only and rest is working fine.even when i logged in from that client on my machine it worked fine too... so the issue was raised only on a particular system or on a particular condition. Thanks! – Umara A. Zahid Jan 03 '17 at 05:51
  • Have does this issue exists particular to your application? Have you checked it with some other application? Try to deploy simple application and check whether viewstate issue persist in that as well? – Dinesh Prajapati Jan 03 '17 at 08:00
  • @DineshPrajapati i did try that with other apps too and all worked fine... and in my application too it only produces problem on one page i.e staff.aspx. and problem is not frequent but generated some times. i manually assigned value to pageStatePersister.Viewstate=null and then i got the same exception. but i don't understand why my client is getting null against this variable. Thanks! – Umara A. Zahid Jan 03 '17 at 08:06
  • You mean to say this exception occurs only on one page and that too random? Can you identify the behavior / pattern which raises the exception. – Dinesh Prajapati Jan 03 '17 at 08:13
  • @DineshPrajapati exactly ... not whole application but page is crashed.... moreover page runs fine but an exception email is recieved everytime... the sequence was as following... i loaded page.. no email... i sorted my grid... no email... refreshed/searched page... recieved exception email.thanks! – Umara A. Zahid Jan 03 '17 at 10:10
  • Exception receives everytime you follow these steps? – Dinesh Prajapati Jan 03 '17 at 10:43
  • Can you add the code for `SavePageStateToPersistenceMedium`? – grek40 Jan 03 '17 at 12:27
  • @grek40 Yes Sure. Please Check My Edited Post. Thanks! – Umara A. Zahid Jan 03 '17 at 12:36
  • Ok, so unless some exceptions are thrown, `pageStatePersister.ViewState` should never be `null` on `Save`. Are you using the default `HiddenFieldPageStatePersister` or some other mechanism? Also, you mention page refresh - refreshing is not a post back, so the ViewState might be lost there, I'm not sure how it is working in detail. – grek40 Jan 03 '17 at 13:11
  • @grek40 Thanks for the details/information you shared. i am using default HiddenFieldPageStatePersister .i have debugged it all ways possible and must say i didnt get any exception. even on my page refresh too .. it restored the viewstate... i am having a headache now because of this strange exception. Thanks! – Umara A. Zahid Jan 03 '17 at 13:42
  • Can you check the traffic on client side? Specifically, inspect the `__VIEWSTATE` value that is sent on `POST` and compare it to previously generated values. If I use firefox and erase the whole viewstate node, the `Load...` method is not called, but if I erase the viewstate value from the DOM node, I get a `null` value on server side where your exception is thrown. So somehow you have to find out where exactly the viewstate is lost and you have to include the client machine in this search. – grek40 Jan 04 '17 at 12:21

0 Answers0