NullReferenceException in arrayList

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
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){

}
 
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.
 
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){


}
 
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)
{

}

}
 
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);
 
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
 
Back
Top