By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

null reference exception in function

P: 20
hi i'm recieveing something called a null reference exception whenever i try to run my code. i am testing for a null value and if it is null i carry out some action, if false i recursively call the same function using the next EVENT array of my custom event class using a basic if, else statement. instead of testing for != null i have also tried testing for == null but get the same response. code is below, also the function is supposed to return an array of long's (returnid's) using recursion. will this work??? thanks.

public long[] getchildeventids(Event[] ukracelist)
{
long[] returnids = null;
if (ukracelist[0].Events != null)
{
getchildeventids(ukracelist[0].Events);
}
else
{
returnids[0] = ukracelist[0].EventId;
return returnids;
}
return returnids;
}
May 30 '09 #1
Share this Question
Share on Google+
7 Replies


100+
P: 344
You've to problems, I can see :
1- Your function should accept Event array, but you send an Event instead.
public long[] getchildeventids(Event[] ukracelist)
getchildeventids(ukracelist[0].Events);
2- The long array returnids is not initialized, it still null. You've to initialize it.
long[] returnids = null;
returnids[0] = ukracelist[0].EventId;
Thanks,
Bassem
May 30 '09 #2

P: 20
ok my second bite of the cherry using a simple boolean function still gives me a unhandled nullexception from "returnids[counter] = i.EventId;" if someone could give me some pointers that would be great. hopefully this one has more chance of returning an actual value in the array than my $%^& poor attempt before lol. thanks in advance.

public long[] getchildeventids(Event[] uklist, int counter)
{

long[] returnids = null;
foreach (Event i in uklist)
{

if (bottomnode(i))
{
returnids[counter] = i.EventId;
counter++;
return returnids;
}
else
{
getchildeventids(i.Events, counter);
}
}

return returnids;
}

public bool bottomnode(Event test)
{
if (test.Events == null)
return true;
else
return false;

}
May 30 '09 #3

100+
P: 344
You tried to fix the first problem, what about the second one?
Initialize your array first!
Expand|Select|Wrap|Line Numbers
  1. int [] a = new int [5];
<<Edited>>
You can use List for unknown array's length.
May 30 '09 #4

10K+
P: 13,264
As if by magic, there exists a whole article dealing with just that exception. What are the odds?
Jun 1 '09 #5

100+
P: 344
This is very helpful article. The first thing I tried to do is to post a link for this article but I couldn't find it. I already read it before and helped me much. It is an excellent article.

Thanks a lot,
Jun 1 '09 #6

P: 20
Thanks for the replies. finally figured out the problem with null reference. i haven't had chance till now to look back at this. but finally realised i needed a dynamic collection which cries out for arraylist and my recursive call to the function was all wrong as each time the method was called a new array/arraylist was created which overwrote the data. it now works with code below. does it seem robust or could there be problems if i use this code? also could someone tell me how to use a new declaration inside a function call e.g.
getchilevents(newevents, (arraylist p = new arraylist());
thanks.

public ArrayList getchildeventids(Event[] uklist, ArrayList ids)
{
foreach (Event i in uklist)
{
if (i.Events == null)
{
ids.Add(i.EventId);
}
else
{
getchildeventids(i.Events, ids);
}
}
return ids;
}
Jun 3 '09 #7

100+
P: 344
also could someone tell me how to use a new declaration inside a function call e.g.
getchilevents(newevents, (arraylist p = new arraylist());
I think it depends, if you'll not use p again inside the method you declared it in, you can do this
Expand|Select|Wrap|Line Numbers
  1. getchilevents(newevents, new arraylist());
But if you'll use it again, declare it first then pass it to the method you call.

Regards,
Bassem
Jun 3 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.