blah blah blah is here! blah blah » Close

up0down
link

Hi all,

I have a weird message coming back from an Exe I wrote an am now debugging, stream lining etc. It returns some message boxes which I would expect and then "Object reference not set to an instance of an Object" in a message box I have seen this or similar when I was messing with an Access database. I believe it was trying to shoe horn a string into a numeric field. The program is based on serial comms, no databases involved!

Glenn

last answered one year ago

3 answers

up1down
link

This error message typically means that you have a 'reference type' variable which is set equal to null and you're then trying to access a property or call a method using that variable. For example:

private string someString; // set to null by default


private void SomeMethod();
{
int index = somestring.IndexOf("a"); // null reference exception
// do something with index
}

The fix, of course, is to make sure that someString isn't null before you call the IndexOf() method on it or, if you can't be sure of this, enclose the code in a try/catch block.

up1down
link

The variable you tried to use was pointing to nothing... Reference type variables require you to create the object they point to before you use them... trying to use a variable that has no object assigned to it will throw that exception.

up0down
link

Hi to all,

Wrote this question when I had more than one thing on the go (12-15 actually). I did figure this out myself, Thanks as always though!

Glenn

Feedback