473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

null reference exception in function

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
7 2788
Bassem
344 100+
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
pinman
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
Bassem
344 100+
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
r035198x
13,262 8TB
As if by magic, there exists a whole article dealing with just that exception. What are the odds?
Jun 1 '09 #5
Bassem
344 100+
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
pinman
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
Bassem
344 100+
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

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

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
2
by: Shannon | last post by:
I am having a very stange issue happen to my web application. I am trying to develop a simple shopping cart. The code works perfectly when I run the application under http://localhost/myapp. ...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
5
by: Boniek | last post by:
Hi I define a public property in a new form and I can see this property in table of Properties in Visual. How I can hide this property to see only in code ? Thank's Boniek
2
by: Marshall Belew | last post by:
My stack trace contains: System.NullReferenceException: Object reference not set to an instance of an object. but the line of code it refers to looks like this: if (null == unit || !...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.