-1

In the code below, the line

foreach (PSObject d in (PSObject[])result.Members["description"].Value)

is causing an exception. "Object reference not set to an instance of an object." Looking through the debugger, that is because there is no element at ["description"]. How can I check to see if an element is there before attempting to get it?

foreach (PSObject result in psInstance.Invoke())
            {
                if (result != null)
                {
                    string pName = result.Members["name"].Value.ToString();
                    string pType = result.Members["parameterValue"].Value.ToString();

                    StringBuilder paramDesc = new StringBuilder();
                    foreach (PSObject d in (PSObject[])result.Members["description"].Value)
                    {
                        paramDesc.Append(d.Members["Text"].Value);
                    }

                    PsAdtParameter param = new PsAdtParameter();
                    param.Description = paramDesc.ToString();
                    param.Name = pName;
                    param.Type = pType;
                    command.Parameters.Add(param);
                }
            }

I know how to check for null. But trying this doesn't work:

foreach (PSObject d in (PSObject[])result.Members["description"].Value)
{
    if(d != null)
    {
        //Do something

Because the reference to Members["description"] causes the exception. Do I have to loop through the array and check each Name property to see if it is "Description"?

Dbloom
  • 1,302
  • 3
  • 18
  • 45
  • 3
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Sep 02 '15 at 19:44
  • That does't work because you have to test if `result.Members["description"]` is `null` before the `foreach`. And maybe even check if `Value` is `null` as well. – juharr Sep 02 '15 at 19:57

3 Answers3

1

A simple null check will suffice in that case. Here are some docs and a good overflow thread on the subject.

var memberDescription = result.Members["description"] as PSObject[];

if (memberDescription != null && memberDescription.Value != null){
    foreach (PSObject d in memberDescription.Value )
    {
        paramDesc.Append(d.Members["Text"].Value);
    }
}
Community
  • 1
  • 1
  • 3
    I am pretty sure this will cause the exception as well because it is thrown when accessing the Value-Property of `null` – Breeze Sep 02 '15 at 19:53
  • You're right. I changed my code to reflect my silly assumption. Ugly to double null check but this should function. – Daniel Hoffmann-Mitscherling Sep 02 '15 at 19:57
  • The cast applies to the `Value`, so now your throwing a cast exception instead. – juharr Sep 02 '15 at 19:58
  • Could you explain the cast exception you expect to run into? It should return as null now. – Daniel Hoffmann-Mitscherling Sep 02 '15 at 20:00
  • This is what I ended up using. Thank you. I know this was a basic question that has probably been answered before, but for whatever reason this made sense to me more than other answers I had read. – Dbloom Sep 02 '15 at 20:09
  • in the OP's code `(PSObject[])result.Members["description"].Value` the cast is being applied to `result.Members["description"].Value` not `result.Members["description"]`. – juharr Sep 02 '15 at 20:10
  • This makes no sense. If `result.Members["description"]` is the array of `PSObject` then `memberDescription.Value` will not compile because arrays do not have a `Value` member. – juharr Sep 02 '15 at 20:12
1

Here's the checks you need to do.

if(result.Members["description"] != null)
{
    var psobjects = result.Members["description"].Value as PSObject[];
    if(psobjects != null)
    {
        foreach (PSObject d in psobjects)
        {
            // whatever you want to do with each PSObject
        }
    }
    else
    {
        // Either Value was null or it wasn't a PSObject array.
    }
}
else
{
    // whatever you want to do when it's null
}

This will additionally make sure that Value is an array of PSObject.

juharr
  • 31,741
  • 4
  • 58
  • 93
0

Check to see if it's null first:

if ((PSObject[])result.Members["description"] != null)
{
     *** Do whatever ***
}
crosstec
  • 302
  • 3
  • 9
  • if performance is a critical thing the result of `(PSObject[])result.Members["description"]` should be stored in a variable before checking and eventually processing it – Breeze Sep 02 '15 at 19:51
  • 1
    I believe the cast actually applies to the `Value`, so this will just throw a cast exception. – juharr Sep 02 '15 at 19:53