ListBox.Items.Clear() causing System.NullReferenceException...

  • Thread starter William F. Robertson, Jr.
  • Start date
W

William F. Robertson, Jr.

Would you post your code for lstProduct_SelectedIndexChanged()?

bill

Hello, Folks!!

Here's the problem:
.. ASP.NET 1.1
.. Running on Windows Server 2000
.. Using WebMatrix to write the code...and sometimes Notepad!
.. 2 Web Objects (ListBoxes) in question
.. Information pulled from MS-SQL server through DataReader

In my application / page I have a listbox, lstCategory, that is
automatically populated when the page is loaded. Based on the selection
from lstCategory it will fill another listbox, lstProduct. When the user
selects from lstProduct it goes and does other things.

When my application tries to populate lstProduct after selecting fom
lstCategory I get the error, "System.NullReferenceException. Object
reference not set to an instance of an object. An unhandled exception..."

With the stack trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
Product.lstProduct_SelectedIndexChanged(Object sender, EventArgs e) +49
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
+108

System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.RaisePo
stDataChangedEvent() +26
System.Web.UI.Page.RaiseChangedEvents() +115
System.Web.UI.Page.ProcessRequestMain() +1081

What the Fudge!?!

I have tried searching for a solution on various sites but no luck, yet.
I know what triggers the problem. Before I populate lstProduct I perform
the command lstProduct.Items.Clear() to clear the list. When I comment out
the line it works fine but keeps adding to lstProduct with a whole bunch of
values that shouldn't be there. Any insights are welcome.

TIA.

Here's some of the code:

' Product.vb
'

Imports Microsoft.VisualBasic
Imports blah, blah, blah.

Public Class Product
'For PostBack
Inherits Page

Protected WithEvents lstCategory as ListBox
Protected WithEvents lstProduct as ListBox
 
W

William F. Robertson, Jr.

Jeremy's explanation was a good one. If there is nothing selected in the
list box the SelectedIndex will be -1. That entails SelectedItem will be
Nothing, and Nothing does not have a property named Value so it blows up.

As far as what it takes to get a -1 for SelectedIndex in the SelectedIndex
changed event. I could only reproduce it two ways.

1) You have a multi select, and the user is ctrl-clicking the last one
selected, thus clearing out their selection.
2) Somewhere in your logic you are calling your FillProductList() or
otherwise repopulating the data.
or (I didn't test this one.)
3) If you turned off viewstate for the page or controls, I believe it will
hiccup.

bill


Addendum:

Okay, I have placed an If statement in lstProduct_SelectedIndexChanged()
that reads as follows:

Private Sub lstProduct_SelectedIndexChanged()
If NOT(lstProduct.SelectedIndex = -1) Then
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top