Object reference not set to an instance of an object.

Posted by Sunitajadhav under ASP.NET on 1/12/2010 | Views : 3529 | Status : [Member] | Replies : 2
Hello friends,
I got this error so i m not getting ouput.What should i do?
Thanks in adavance.

Server Error in '/DSSProjectScheduling' Application.

--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 21: int t_ModelID = 0;
Line 22: t_ModelID = Convert.ToInt32(Session["t_ModelID"]);
Line 23: m_ProjectString =Convert.ToString(Session["t_ProjectString"].ToString());
Line 24: m_ModelID = t_ModelID;
Line 25: LabelReport.Text = ProjectList.m_SerialOptimalSolution;

Source File: c:\Users\Suresh\Documents\Visual Studio 2005\WebSites\DSSProjectScheduling\WebSitePages\OptimalReport.aspx.cs Line: 23

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
OptimalReport.Page_Load(Object sender, EventArgs e) in c:\Users\Suresh\Documents\Visual Studio 2005\WebSites\DSSProjectScheduling\WebSitePages\OptimalReport.aspx.cs:23
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

________________________________________
Version Information: Microsoft .NET Framework Version:2.0.50727.1434; ASP.NET Version:2.0.50727.1434




Responses

Posted by: Rcsprakash on: 1/12/2010 [Member] Starter

Up
0
Down
that line you are converting the session value into string

you are using both .ToString() and Convert.ToString()

if session has null values .ToString() throws the error you received. Convert.ToString() can work with null values also.
so remove .toString() then try.

Sunitajadhav, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 1/12/2010 [Member] Bronze

Up
0
Down
sunita,

In your code I can figure out both ToString() and Convert.ToString methods in the highlighted place below.

Line 21: int t_ModelID = 0;
Line 22: t_ModelID = Convert.ToInt32(Session["t_ModelID"]);
Line 23: m_ProjectString =Convert.ToString(Session["t_ProjectString"].ToString()); Line 24: m_ModelID = t_ModelID;
Line 25: LabelReport.Text = ProjectList.m_SerialOptimalSolution

Actually only Convert.ToString is required to handle the conversions. Always use Convert.ToString instead of ToString() method. Because ToString() never handle null values.

So kindly modify your code as below.

Line 21: int t_ModelID = 0; 

Line 22: t_ModelID = Convert.ToInt32(Session["t_ModelID"]);
Line 23: m_ProjectString =Convert.ToString(Session["t_ProjectString"]);
Line 24: m_ModelID = t_ModelID;
Line 25: LabelReport.Text = ProjectList.m_SerialOptimalSolution


Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sunitajadhav, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response