NullReferenceException in arrayList

P

puzzlecracker

Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an ob
ject.

Here is where I get this:

public void Foo(){

ArrayList list=new ArrayList();
Bar(list); //<== This throws the exception
}

public void Bar(ArrayList list){

}
 
J

Jon Skeet [C# MVP]

puzzlecracker said:
Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an ob
ject.

Here is where I get this:

public void Foo(){

ArrayList list=new ArrayList();
Bar(list); //<== This throws the exception
}

public void Bar(ArrayList list){

}

Well, the code that you've given won't do that. It must be some of the
code that you haven't shown.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
P

puzzlecracker

Well, the code that you've given won't do that. It must be some of the
code that you haven't shown.

Could you post a short but complete program which demonstrates the
problem?

Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfor details of
what I mean by that.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

sorry it's bit more complicated:
public class A{

public event onBarHandler onBar;

public void Foo(){
ArrayList list=new ArrayList();
onBar(list); //<== This throws the exception

}

public void onBarHandler (ArrayList list){


}
 
P

puzzlecracker

It's a bit more complicated:

public class A{

public event onBarHandler onBar;

public void Foo()
{
ArrayList list=new ArrayList();
onBar(list); //<== This throws the exception and I pass empty
list....
}

public void onBarHandler(ArrayList list)
{

}

}
 
A

Alberto Poblacion

puzzlecracker said:
public event onBarHandler onBar;
[...]
onBar(list); //<== This throws the exception and I pass empty
list....

No, the exception is not caused by the empty list; it is caused by the
empty onBar. You have defined an event "onBar", and you are trying to raise
the event without it having been hooked by anyone. When raising an event you
should first verify that a client has connected to the event:

if (onBar!=null) onBar(list);
 
J

Jibesh

Again, Exception is not because of Empty ArrayList, Its purely because of
null value in onBar delegate event.

public class A{

public event onBarHandler onBar;

public void Foo()
{
ArrayList list=new ArrayList();
onBar = onBarHandler; // <-- will solve the exeption.
onBar(list); //<== This throws the exception and I pass empty
list....
}

public void onBarHandler(ArrayList list)
{

}

Thanks
Jibesh
 

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