Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Accusoft 58 posts 109 karma points
    May 05, 2011 @ 15:16
    Accusoft
    0

    Object Error when dealing with Documents in API

    I'm working on a recursive method that displays all documents I have permissions to see.  The first pass works great, but when it calls itself recursively passing a document array of the current document's children it throws an error:

     

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

     

    Here's the code:

     

    protected void Page_Load(object sender, EventArgs e)
            {
                lblTest.Text = "Data<br /><br />";
                Document[] releaseDocs = Document.GetRootDocuments();
                displayDocs(releaseDocs);
            }
    public void displayDocs(Document[] releaseDocs)
            {
                string docPermissions = null;
                User currentUser = User.GetCurrent();
                foreach (var doc in releaseDocs)
                {
                    docPermissions = currentUser.GetPermissions(doc.Path);
                    if ((docPermissions.Contains("F")) && (docPermissions.Contains("U")))
                    {
                        lblTest.Text += "D/T: " + doc.CreateDateTime + "<br />\r\n";
                        lblTest.Text += "Level: " + doc.Level + "<br />\r\n";
                        lblTest.Text += "Text: " + doc.Text + "<br />\r\n";
                        lblTest.Text += "<hr />\r\n";
                        if (doc.HasChildren)
                        {
                            Document[] childDocs = Document.GetChildrenForTree(doc.Id);
                            displayDocs(childDocs); //error occurs here
                        }
                    }
                }
            }

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 08, 2011 @ 23:28
    Bo Damgaard Mortensen
    0

    Hi Chris,

    Is this still buggin' you? :-)

    Have you checked to see if theres actually any documents in the childDocs array? Alternatively, if that's the case, you should be able to get the child documents like this: doc.Children;

    / Bo

    Edit: Having a look at your code again, I think it will be good practice to "control" recursive methods with a few parameters. In this case, which node will be the start node? And when you meet a node with children, will that node be the new start node? When there is finally no more child collections, which node did you come from at the first place? Etc etc :-)

    Maybe it would be better to refactor the code and make a helper method that iterates through childcollections? After all, everything that could be done recursively, can also be done with seperate iterations ;-) Just a thought..

  • Accusoft 58 posts 109 karma points
    May 09, 2011 @ 13:41
    Accusoft
    0

    Hi Bo,

    Thanks for the reply.  I built a workaround that passed the ID of the current node instead of the array of nodes.  This seems to work better, though I'm not 100% sure why.  Regardless, it got the job done.  Thanks for your followup!

     

Please Sign in or register to post replies

Write your reply to:

Draft