null reference exception in function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pinman
    New Member
    • Aug 2008
    • 20

    null reference exception in function

    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[] getchildeventid s(Event[] ukracelist)
    {
    long[] returnids = null;
    if (ukracelist[0].Events != null)
    {
    getchildeventid s(ukracelist[0].Events);
    }
    else
    {
    returnids[0] = ukracelist[0].EventId;
    return returnids;
    }
    return returnids;
    }
  • Bassem
    Contributor
    • Dec 2008
    • 344

    #2
    You've to problems, I can see :
    1- Your function should accept Event array, but you send an Event instead.
    public long[] getchildeventid s(Event[] ukracelist)
    getchildeventid s(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

    Comment

    • pinman
      New Member
      • Aug 2008
      • 20

      #3
      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[] getchildeventid s(Event[] uklist, int counter)
      {

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

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

      return returnids;
      }

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

      }

      Comment

      • Bassem
        Contributor
        • Dec 2008
        • 344

        #4
        You tried to fix the first problem, what about the second one?
        Initialize your array first!
        Code:
        int [] a = new int [5];
        <<Edited>>
        You can use List for unknown array's length.
        Last edited by Bassem; May 30 '09, 04:37 PM. Reason: Add notation for List

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          As if by magic, there exists a whole article dealing with just that exception. What are the odds?

          Comment

          • Bassem
            Contributor
            • Dec 2008
            • 344

            #6
            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,

            Comment

            • pinman
              New Member
              • Aug 2008
              • 20

              #7
              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(n ewevents, (arraylist p = new arraylist());
              thanks.

              public ArrayList getchildeventid s(Event[] uklist, ArrayList ids)
              {
              foreach (Event i in uklist)
              {
              if (i.Events == null)
              {
              ids.Add(i.Event Id);
              }
              else
              {
              getchildeventid s(i.Events, ids);
              }
              }
              return ids;
              }

              Comment

              • Bassem
                Contributor
                • Dec 2008
                • 344

                #8
                also could someone tell me how to use a new declaration inside a function call e.g.
                getchilevents(n ewevents, (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
                Code:
                getchilevents(newevents, new arraylist());
                But if you'll use it again, declare it first then pass it to the method you call.

                Regards,
                Bassem

                Comment

                Working...