Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

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

Reply
 
Old 08-31-2006   #1 (permalink)
=?Utf-8?B?UGF1bCBC?=


 
 

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
Old 08-31-2006   #2 (permalink)
=?Utf-8?B?Um9tYW4gS3V6bWlu?=


 
 

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
Old 08-31-2006   #3 (permalink)
=?Utf-8?B?UGF1bA==?=


 
 

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
Old 08-31-2006   #4 (permalink)
Alex K. Angelopoulos [MVP]


 
 

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
Old 08-31-2006   #5 (permalink)
Jouko Kynsijärvi


 
 

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
Old 08-31-2006   #6 (permalink)
=?Utf-8?B?UGF1bA==?=


 
 

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
Old 08-31-2006   #7 (permalink)
Alex K. Angelopoulos [MVP]


 
 

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
Old 08-31-2006   #8 (permalink)
Adam Milazzo


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Net.WebRequest.Create objects GetResponse method returns -1.. why? .NET General
Gotcha: $null to [string] IS NOT $null PowerShell
Find objects with a certain property PowerShell
should out-null offer the option of disposing incoming objects? PowerShell
get-adobject only returns 1000 objects PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46