Click to See Complete Forum and Search --> : Object reference not set to an instance of an object


joshdalegend
February 19th, 2006, 07:39 PM
I have the following code in a code behind file:

----------------------------DisplayList.aspx.vb------------------------------------------
Public Class DisplayListCodeBehind
Inherits System.Web.UI.Page

Protected WithEvents pageSectionAdd As System.Web.UI.WebControls.Button
Protected WithEvents pageSectionRemove As System.Web.UI.WebControls.Button
Protected WithEvents pageSectionOptions As System.Web.UI.WebControls.ListBox
Protected WithEvents pageSectionCurrent As System.Web.UI.WebControls.ListBox


public Sub setSessions
pageSectionOptions.items.clear
pageSectionCurrent.Items.clear


if NOT Session("sectionA") = "false" then
...........
...........
...........

End Sub
End Class

------------------------------------------------------------------------------------------

Within my main page file, i have the following code:

--------------------------page.aspx-----------------------------------------------------


...........
...........
...........
<td>
<asp:ListBox runat="server" SelectionMode="Single" id="pageSectionOptions">
</asp:ListBox>
</td>

<td>
<asp:Button id="pageSectionAdd" width="100" text="&gt;&gt;Add&gt;&gt;" runat="server" />
<br />
<asp:Button id="pageSectionRemove" width="100" text="&lt;&lt;Remove&lt;&lt;" runat="server" />

</td>

<td>
<asp:ListBox runat="server" width="200" SelectionMode="Single" id="pageSectionCurrent">
</asp:ListBox>
</td>
...........
...........
...........
...........
-------------------------------------------------------------------------------------------


But i'm getting the following error, can anyone explain why???

----------------------------------------error----------------------------------

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 22:
Line 23: public Sub setSessions
Line 24: pageSectionOptions.items.clear
Line 25: pageSectionCurrent.Items.clear
Line 26:


Source File: \\genfs2\www40\joshgosselin\CodeBehind\SubDisplayList.aspx.vb Line: 24

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
DisplayListCodeBehind.setSessions() in \\genfs2\www40\joshgosselin\CodeBehind\SubDisplayList.aspx.vb:24
DisplayListCodeBehind.Page_Load(Object sender, EventArgs e) in \\genfs2\www40\joshgosselin\CodeBehind\SubDisplayList.aspx.vb:18
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750



-----------------------------------------------------------------------------------------


Many Thanks in advance

Josh

mmetzger
February 19th, 2006, 07:41 PM
You can't rely on a Session variable staying in memory. You need to check first if the variable is null - if so, reload it / etc.