473,804 Members | 2,243 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 3768
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
2898
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
2256
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
8426
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
2273
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
10069
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
2301
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
3995
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
4033
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
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10600
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...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
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
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.