Initializing list in constructor ObjectNullReferenceException

V

VonGuller

Hello,

I have a code like this(as an example) :

public class class1:UserControl
{
private MyOwnCollectionBasedClass pList1;

public MyOwnCollectionBasedClass List1
{
get{ return pList1;}
set{pList1 = value;}
}

public class1()
{
pList1 = new MyOwnCollectionBasedClass()
}

private void class1_load(object sender, EventArgs e)
{
LoadMethod();
}

private void LoadMethod()
{
int count = List1.Count;
}

}

This is a user control on a form docked as fill.

In Most of the computers this works ok, but on some computers this
crashes on LoadMethod(). For somereason the List1 is null and there
comes ObjectNullReferenceException:Object reference not set to an
instance of an object.

Why is List1 null? It is initialized in the constructor...The
MyOwnCollectionBasedClass constructor doesn't have any code...so it
should happen fast.

Regards, Jyri
 
M

Marc Gravell

The speed of the ctor shouldn't be an issue anyway...

Are you *sure* that there is no code (possibly later in the ctor of your
actual class, possible between "new" and "Show") that might fire through
some 'orrible maze of code and end up setting pList1 to null?

IMO, since you inner code relies on List1 not being null, and you have an
unckecke setter, you should (one of):
* Change all code to anticipace possible nulls, and handle gracefully
(usually by a short-circuit)
* Change the setter to throw an ArgumentNullException if value == null

Actually, the latter, along with the call start on a break-point, might help
you track the offender...

Marc
 
G

Guest

You pList1 initialization works wrong in some cases, apparently. Just con
what can be the reason into MyOwnCollectionBasedClass
Insert validation, before using pList1 that your pList1 was initialized
successfully

--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Missed that MyOwnCollectionBasedClass doesnt have any code in constructor.
What's the MyOwnCollectionBasedClass at all?

--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
V

VonGuller

MyOwnCollectionBasedClass is a CollectionBase inherited list that holds
certain type of objects. You could say it's typed ArrayList.
 
G

Guest

Hmm,does this behaviour have place in debug or release mode?

It's hard to say what's wrong exactly, because from that perspective there
are no errors

Just add error handles to be sure that everything ok.
And as Mark said in case of exception con your call stack.

Another solution to find out what's wrong is to use SOS, to see the
behaviour of your class - does it really into root and was not GCed
--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

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

Top