Windows Vista Forums

Objects property returns null, but it isn't....
  1. #1


    =?Utf-8?B?UGF1bCBC?= Guest

    Objects property returns null, but it isn't....

    I've encountered a really strange problem trying to access a decimal property
    on an object that is returned from a cmdlet I've built.

    If I print out the object to the console, it shows all the members populated
    and correct. However, if I try and access a property using the ".", it
    returns nothing.

    e.g.

    $myObject = my-cmdlett
    $myObject # prints out object details to console, everything correct.
    $myObject.amount # returns nothing.

    amount is a public property, is a decimal, and is populated with 10.0.



    I've used "." all over my script to access properties and have had this
    issue pop up quite often. Usually refactoring the code and just general trial
    and error fixes the problem - but I'd really like to know why this happens,
    especially as this time I don't seem to be able to solve it.

    Anyone have an idea?

    Thanks.

      My System SpecsSystem Spec

  2. #2


    =?Utf-8?B?Um9tYW4gS3V6bWlu?= Guest

    RE: Objects property returns null, but it isn't....

    Are you sure that your result $myObject is a single object and not an array?
    If it is an array see the thread "Confused getting properties of Collections
    of one vs. many" for similar discussion and answers.

    --
    Thanks,
    Roman


      My System SpecsSystem Spec

  3. #3


    =?Utf-8?B?UGF1bA==?= Guest

    RE: Objects property returns null, but it isn't....

    It's definately a single object.

    I proved this by trying to index it ($object[0]) - it returned null.

    "Roman Kuzmin" wrote:

    > Are you sure that your result $myObject is a single object and not an array?
    > If it is an array see the thread "Confused getting properties of Collections
    > of one vs. many" for similar discussion and answers.
    >
    > --
    > Thanks,
    > Roman
    >


      My System SpecsSystem Spec

  4. #4


    Alex K. Angelopoulos [MVP] Guest

    Re: Objects property returns null, but it isn't....

    Paul, that indicates that you do have something there, a null value. Here's
    an example of what I see attempting to index into a single-valued variable
    and one that has a null value in the indexed location:
    PS> $foo = 1
    PS> $foo[0]
    Unable to index into an object of type System.Int32.
    At line:1 char:6
    + $foo[0 <<<< ]
    PS> $foo = $null,1
    PS> $foo[0]
    To confirm this, try getting the Length or Count on the output from the
    cmdlet.


    "Paul" <Paul@discussions.microsoft.com> wrote in message
    news:BAB4F308-D502-4C3D-B6D8-3302512DEA01@microsoft.com...
    > It's definately a single object.
    >
    > I proved this by trying to index it ($object[0]) - it returned null.
    >
    > "Roman Kuzmin" wrote:
    >
    >> Are you sure that your result $myObject is a single object and not an
    >> array?
    >> If it is an array see the thread "Confused getting properties of
    >> Collections
    >> of one vs. many" for similar discussion and answers.
    >>
    >> --
    >> Thanks,
    >> Roman
    >>




      My System SpecsSystem Spec

  5. #5


    Jouko Kynsijärvi Guest

    Re: Objects property returns null, but it isn't....

    Paul wrote:
    > It's definately a single object.
    >
    > I proved this by trying to index it ($object[0]) - it returned null.


    Then something is definitely wrong, since indexing a non-collection should
    return an error like "Unable to index into an object of type X".

    To check if you really got back a single object with correct type, try this:

    $myObject = my-cmdlett
    $myObject.gettype().fullname

    If it is returning back an array (like System.Object[]), dump the contents
    with this:

    for ($i = 0; $i -lt $myObject.length; $i++) { "$i=$($myObject[$i])" }

    I'm suspecting that for some reason you are actually getting back an array
    with two items: $null and your object.



      My System SpecsSystem Spec

  6. #6


    =?Utf-8?B?UGF1bA==?= Guest

    Re: Objects property returns null, but it isn't....

    You guys rock my world. You're right. I have no idea *how* that variable has
    become an array because looking at the code it all seems fine. I now know
    what's wrong though and can work on fixing it.

    Thanks.

    "Jouko Kynsijärvi" wrote:

    > Paul wrote:
    > > It's definately a single object.
    > >
    > > I proved this by trying to index it ($object[0]) - it returned null.

    >
    > Then something is definitely wrong, since indexing a non-collection should
    > return an error like "Unable to index into an object of type X".
    >
    > To check if you really got back a single object with correct type, try this:
    >
    > $myObject = my-cmdlett
    > $myObject.gettype().fullname
    >
    > If it is returning back an array (like System.Object[]), dump the contents
    > with this:
    >
    > for ($i = 0; $i -lt $myObject.length; $i++) { "$i=$($myObject[$i])" }
    >
    > I'm suspecting that for some reason you are actually getting back an array
    > with two items: $null and your object.
    >
    >
    >


      My System SpecsSystem Spec

  7. #7


    Alex K. Angelopoulos [MVP] Guest

    Re: Objects property returns null, but it isn't....

    I don't know what the cmdlet is working on, but we've had prior discussions
    about how the COM adapter, for example, was returning a null instead of
    nothing at all when calling methods that had a void return type. Check for
    method calls in your code that don't send a result somewhere specific, and
    try explicitly casting them to void.

    "Paul" <Paul@discussions.microsoft.com> wrote in message
    news:680A06CA-4E97-452B-856D-D60AB85E96A8@microsoft.com...
    > You guys rock my world. You're right. I have no idea *how* that variable
    > has
    > become an array because looking at the code it all seems fine. I now know
    > what's wrong though and can work on fixing it.
    >
    > Thanks.
    >
    > "Jouko Kynsijärvi" wrote:
    >
    >> Paul wrote:
    >> > It's definately a single object.
    >> >
    >> > I proved this by trying to index it ($object[0]) - it returned null.

    >>
    >> Then something is definitely wrong, since indexing a non-collection
    >> should
    >> return an error like "Unable to index into an object of type X".
    >>
    >> To check if you really got back a single object with correct type, try
    >> this:
    >>
    >> $myObject = my-cmdlett
    >> $myObject.gettype().fullname
    >>
    >> If it is returning back an array (like System.Object[]), dump the
    >> contents
    >> with this:
    >>
    >> for ($i = 0; $i -lt $myObject.length; $i++) { "$i=$($myObject[$i])" }
    >>
    >> I'm suspecting that for some reason you are actually getting back an
    >> array
    >> with two items: $null and your object.
    >>
    >>
    >>




      My System SpecsSystem Spec

  8. #8


    Adam Milazzo Guest

    Re: Objects property returns null, but it isn't....

    Paul wrote:
    > You guys rock my world. You're right. I have no idea *how* that variable has
    > become an array because looking at the code it all seems fine. I now know
    > what's wrong though and can work on fixing it.
    >
    > Thanks.


    More evidence in favor of Mike's complaint... :-P

      My System SpecsSystem Spec

Objects property returns null, but it isn't.... problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
win32_mappedlogicaldisk returns null on Vista daveL PowerShell 5 24 Feb 2010
Net.WebRequest.Create objects GetResponse method returns -1.. why? buu .NET General 0 25 Apr 2008
Find objects with a certain property rb_acm@yahoo.com PowerShell 4 11 May 2007
should out-null offer the option of disposing incoming objects? Oisin Grehan PowerShell 1 02 Apr 2007
get-adobject only returns 1000 objects Rob Campbell PowerShell 4 16 Mar 2007