Resolved Object reference not set - particular data?

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
I was wondering whether anyone had any idea why only one piece of data would throw this reference.

I am downloading a view from a SQL, so far all the entries have been working absolutely fine, but then I pick 1 item (out of 9,500) and get thrown this error.

I have checked the SQL and there is data there in this format:

X12345 - Forumxy-Abcdefg-AbcDefgAbcdef-Abcdefgh

There are no different characters, no foreign characters, no umlauts or anything of that nature. Just wondering if anyone can see why this data might throw an excepting in C# - are there too many chars, too many -

I just do not see why this particular piece of data, out of so many, would throw an exception and I am sure that there are longer strings in the data so a little bit lost at the moment.

The data is populating a ListBox which when doubleclicked populates a TextBox. The code is pretty simplistic and works on the other pieces of data, so I do not think it is a coding problem but it is below if you need it.

C#:
private void listBox2_DoubleClick(object sender, EventArgs e)
        {
            hbox.Text = listBox2.SelectedValue.ToString();
            pbox.Text = listBox1.SelectedValue.ToString();
            tl.Text = listBox3.SelectedValue.ToString();
        }
 
Solution
Well this works:

C#:
public void listbox1.DoubleClick(object sender, EventArgs e)
{
    string code = (string)listBox1.SelectedValue;
    textBox1.Text = code.Trim();
}

The hours wasted because someone didn't normalise their data.....
It seems fairly obvious that the SelectedValue for one of those ListBoxes is null when that code is executed. The obvious first step is determine which one. The obvious second step is to look at the SelectedItem to see whether anything is selected and, if so, what exactly it contains. For a SelectedValue property to be null, at least one of the following must be true:
  1. The ValueMember was not set.
  2. No item is selected.
  3. The selected item contains null in the property specified by the ValueMember.
You need to determine which it is so you know what problem you're trying to solve.

BTW, what type is the property specified by the ValueMember? If it is String then you ought to be casting as type String rather than converting by calling ToString. You can cast null as type String without issue.
 
That is some really good advice there JMC, as always, casting it negates the exception failure.

As to the underlying issue.

This is tricky, the vast majority of other examples work (except for another one I just found with the same layout with all the dashes ( - ) in the name)

I don't think it is a setting, such as ValueMember (though I did check - and that is set).

The ValueMember must be sending me back a null....but here is the weird part....I have filtering on the list from the Unique ID - and if you filter down listbox1 by the Unique ID, listbox2 filters down as well - and displays the Name as set out in my first post in the listbox.

It is only when you double click (either the unique ID or the name listboxes) asking it to populate the textbox does it throw an exception of null - but it is there to be seen but not to populate.

When I cast to string, and double click, the TextBox is blank - so it is definitely throwing me null data...I just don't know why - the data is there?

edit - forgot to mention there is a selectedItem as the listboxes are linked through databinding....and you can see it is selected, and even if you doubleClick on the name (which you would suppose selects it) it still gives a null output.
 
Last edited:
You seem to be guessing about a lot of things rather than actually debugging your code. You don't need to guess. The debugger will tell you.

Place a breakpoint at the top of that code and then, when the debugger breaks at that line, you can use the tools provided to look at all the relevant values. You can actually look at the SelectedValue of each ListBox to see if one is null and which it is. Once you determine that, you can look at both the SelectedItem and ValueMember of that same ListBox.

If you don't know how to debug then stop what you're doing and learn now. There are numerous tutorials out there and I believe either @Sheepings or @Skydiver has a link to one in their signature. We expect that you will have already debugged your code when you post a question here.
 
Ok, I think the problem is occurring before the ListBox1 Selected Item issue shows up - that is how you say, a symptom of a different problem.

I followed it through with breakpoints, and it SelectedItem and SelectedValue all exist- of course it does, how else does it work for the other 9,000 entries if it didn't exist....but it disappears.

Not because of the double click, not because of any of that behaviour.

If I search for this piece of data, it shows up in ListBox1 (as a Unique ID) and ListBox2 (as a Name) - it is right there, from the Database.

Then if I clear TextBox, effectively clearing the filter, the ListBoxes repopulate.....except, that one piece of data is now missing.

If I search for it, it isn't there. If I scroll through the data, it isn't there, in either ListBox1 or ListBox2. Bear in mind, I didn't touch the data beyond filtering for it. I didn't double click it into a TextBox, I just filtered to it, displayed it as a single item in the ListBox...and then left.

My code for the filter is simply:

bind:
bindingsource.Filter = $"UniqueID LIKE '%{code.Text}%'";

It is this behaviour, I think, that is breaking the DoubleClick to TextBox function...as of course when you DblClick, the TextBox changes, it skips to the TextBoxChanged event to see if it needs to filter...and then when it comes back....the data is gone - not because of this of code - but because of something that I don't really understand....if this happened to every piece of data....that I could understand, that I could work on and fix...but just the odd bit of data? This one, but not that one?

It isn't even a random error, it is the same error on the same pieces of data...but not the data before it, not the data after it...just that piece of data.

As an update, the number of items in the list doesn't change. Just this piece of data becomes invisible for all intents and purposes.
 
Last edited:
When you have an issue like this, it is often a good idea to strip it back to the absolute basics. Create a new project with everything you need to replicate this issue and nothing more. It is easier to concentrate on specific functionality when it is isolated. If you still can't solve the problem, you can provide us with instructions to reproduce the issue using your minimal example. If we can replicate the issue without having to replicate all the irrelevant stuff in your project, we may be able to diagnose the issue for you.
 
Thanks JMC, another good suggestion on troubleshooting, one I will put in my memory banks.

I am not going to post the code, because I see what the problem is....it isn't the code, it is the data - which is what I thought originally.

I was looking at it and I noticed that on this piece of data the number looks like this:

"A12345 "

The number is usually:

"A12346"

It is that space at the end that is causing the problem, because the ListBox doesn't consider the space - even though that is the underlying data, so when you populate the textbox, there is no longer a SelectedItem for the ListBox as the number with a space doesn't exist....this in turn deselects the Name - as they are bound together, so when it comes to populate the TextBox for the name, there is no value, as nothing is selected - it doesn't occur when populating the TextBox for the Number as it populates before doing a filter check

Going to go look at whether I can trim the data so that it is only ever 6 characters in length, either at the point of ingestion or or at the point I populate the textbox - given that this number is never more than 6 characters.
 
Well this works:

C#:
public void listbox1.DoubleClick(object sender, EventArgs e)
{
    string code = (string)listBox1.SelectedValue;
    textBox1.Text = code.Trim();
}

The hours wasted because someone didn't normalise their data.....
 
Solution
The hours wasted because someone didn't normalise their data.....
Get used to it. Data migration is the bane of my existence. My company often create bespoke web applications to replace existing applications and I've never met a database that wasn't a complete disgrace in doing that.
 
And the irony will be after spending many hours normalizing your data, someone else is going to go on a datawarehousing and/or data mining kick -- and for some reason or another they will want to flatten your nicely normalized data because the analysis tools that they use can't handle normalized data.
 
Back
Top Bottom