Null Pointer Exception,Object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swathi amireddy
    New Member
    • Oct 2008
    • 7

    Null Pointer Exception,Object reference not set to an instance of an object

    Currently im trying to migrate a project from .net 1.1 to 2.0.While running the application its giving an exception for the below code while doing the update operation:

    protected void btnUpdate_Click (object sender, System.EventArg s e)
    {
    int ParamID=Convert .ToInt16(Sessio n["ParamID"].ToString());(s howing exception at this line)
    ....
    ....
    }
    private void grdParameters_I temCommand(obje ct source, System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    Session["ParamID"]=e.Item.Cells[0].Text.ToString( );
    .....
    .....
    ....
    }

    The exception that is raised is:
    "Null pointer Exception unhandled by the usercode".Objec t reference not set to an instance of an object.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Which line is reported for the error? You do understand what the exception means, right?

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      I suggest place the session string in a variable and write to check it exists. You want to think about adding some logic should the session variable be empty.

      Comment

      • swathi amireddy
        New Member
        • Oct 2008
        • 7

        #4
        Originally posted by kenobewan
        I suggest place the session string in a variable and write to check it exists. You want to think about adding some logic should the session variable be empty.
        By debugging,we came to know that its getting the NULL value.Can u suggest some code that we need to add to solve the issue??

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Correcting it is as easy as putting a test (if test) for null before dereferencing (.dotting) the value that is null.

          Comment

          • Murugs
            New Member
            • Nov 2007
            • 18

            #6
            int ParamID=0;//Initialize with default value
            if (Session["ParamID"] != null)
            {
            ParamID=Convert .ToInt16(Sessio n["ParamID"]);
            }

            hope this will helps you..

            Comment

            • swathi amireddy
              New Member
              • Oct 2008
              • 7

              #7
              Originally posted by Murugs
              int ParamID=0;//Initialize with default value
              if (Session["ParamID"] != null)
              {
              ParamID=Convert .ToInt16(Sessio n["ParamID"]);
              }

              hope this will helps you..

              Thank you....
              its working....

              Comment

              Working...