473,796 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ListBox.Items.C lear() causing System.NullRefe renceException. ..

Nov 18 '05 #1
4 3767
Would you post your code for lstProduct_Sele ctedIndexChange d()?

bill

"Eeediot" <ee*****@hotmai l.com> wrote in message
news:fI******** ************@gi ganews.com...
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.NullRef erenceException . Object
reference not set to an instance of an object. An unhandled exception..."

With the stack trace:
[NullReferenceEx ception: Object reference not set to an instance of an
object.]
Product.lstProd uct_SelectedInd exChanged(Objec t sender, EventArgs e) +49
System.Web.UI.W ebControls.List Control.OnSelec tedIndexChanged (EventArgs e)
+108

System.Web.UI.W ebControls.List Box.System.Web. UI.IPostBackDat aHandler.RaiseP o
stDataChangedEv ent() +26
System.Web.UI.P age.RaiseChange dEvents() +115
System.Web.UI.P age.ProcessRequ estMain() +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.Item s.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.Visua lBasic
Imports blah, blah, blah.

Public Class Product
'For PostBack
Inherits Page

Protected WithEvents lstCategory as ListBox
Protected WithEvents lstProduct as ListBox
Nov 18 '05 #2
Nov 18 '05 #3
Nov 18 '05 #4
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
"Eeediot" <ee*****@hotmai l.com> wrote in message
news:Ft******** ************@gi ganews.com...
Addendum:

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

Private Sub lstProduct_Sele ctedIndexChange d()
If NOT(lstProduct. SelectedIndex = -1) Then
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2895
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get bunched up at the right, i.e. squashed up! any idea why? The main bit of the code is as such // (in progressReporter.cs) protected struct LBRow //a row of the listbox, whether it be the title or a
2
1765
by: xzzy | last post by:
I have an app that uses a listbox to display program status messages, and a public function to be used by the solution to update the listbox. I do not know how to make the listbox public so it can be updated by that function. public class frmMain : System.Windows.Forms.Form{ public System.Windows.Forms.ListBox listBox1;
10
2255
by: yop | last post by:
All When I try to get the text from my listbox I am get an error which is listed below. Any ideas? Thanks Object reference not set to an instance of an object.
1
8424
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's Address1, Address2 etc. controls with the relevant data items. I put the code for this into the...
1
2272
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the button_click function because the only way to find the listbox is: (listbox)e.item.findcontrol What s the solution to this problem?
7
10068
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if there is already one. C.P.
3
2299
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
3
3994
by: papa smerf | last post by:
'Here is some of the code I have so far. Dim Contacts(25, 4) As String This is where I add to the listbox Private Sub btnAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToList.Click Dim f, l As Integer f = Len(txtFirstName.Text) l = Len(txtLastName.Text)
1
4031
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.