header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
You cannot call a method on a null-valued expression
Last Post 03 Apr 2012 12:51 AM by Sharp Kid. 11 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
icts.prsiUser is Offline
New Member
New Member
Posts:2
Avatar

--
16 Jan 2009 08:41 AM

    Hi, I'm hoping someone can help me here.

    I am trying to use powershell to produce graphical reports but am constantly stumbling on every script that contains this particular line:


        $series = ([array] $chart.charts)[0].SeriesCollection.Add(0)

    It doesnt matter what the script does, either simply collecting process utilisation from the local pc or obtaining management stats from our vmware environment, any script that uses that line causes the following error:


    You cannot call a method on a null-valued expression. At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\test.ps1:18 char:62 + $series = ([array] $chart.charts)[0].SeriesCollection.Add <<<< (0)


    Here's hoping for something really silly/trivial that'll make all my scripts work properly!
    Thanks in advance


    $msWord = New-Object -Com Word.Application
     # Create new document
    $wordDoc = $msWord.Documents.Add()
     # Make word visible (optional)
    $msWord.Visible = $true
    # Activate the new document
    $wordDoc.Activate()
    $Caption = "Simple Example"
    $categories = @()
    $values = @()
    $chart = new-object -com OWC11.ChartSpace.11
    $chart.Clear()
    $c = $chart.charts.Add(0)
    $c.Type = 4
    $c.HasTitle = "True"
    $c.HasLegend = "True"
    $series = ([array] $chart.charts)[0].SeriesCollection.Add(0)
    Get-Process | foreach-object { 
       $categories += $_.Name $values += $_.CPU * 1
        }
    $series.Caption = $Caption
    $series.SetData(1, -1, $categories)
    $series.SetData(2, -1, $values)
    $filename = (resolve-path .).Path + "\pie1.jpg"
    $chart.ExportPicture($filename, "jpg", 800, 500)
    $objSelection = $msWord.Selection
    $msword.Selection.EndKey(6)
    $objSelection.TypeParagraph()
    $msWord.Application.Selection.InlineShapes.AddPicture($filename) > Null

    halr9000User is Offline
    PowerShell MVP, Site Admin
    Advanced Member
    Advanced Member
    Posts:565
    Avatar

    --
    16 Jan 2009 10:47 AM
    Well the error means that you're trying to call a method on "nothing". e.g.

    $null.method()

    Same error. So what you need to do is to examine your code to see why the object in question is not being created or passed on to the right variable.

    P.S. Sorry for the obvious text editor issues, we are working on that. In the meantime, could you try posting your script again? Another option would be to place the script on a pastebin like this one: http://posh.jaykul.com/p
    Community Director, PowerShellCommunity.org
    Co-host, PowerScripting Podcast
    Author, TechProsaic
    icts.prsiUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    19 Jan 2009 02:32 AM

    Hi, here's one of the many scripts I've tried.
    To be honest, I've downloaded so many examples from various websites that all contain the same...
       
       $series = ([array] $chart.charts)[0].SeriesCollection.Add(0)     

     ...line, that I was assuming that it wasnt really a script/code issue although I may be very wrong (I'm new to powershell, vbscript is more my cup of tea)

    Hopefully, this script is a bit more readable...
    Cheers.

    $msWord = New-Object -Com Word.Application
    # Create new document
    $wordDoc = $msWord.Documents.Add()
    # Make word visible (optional)
    $msWord.Visible = $true
    # Activate the new document
    $wordDoc.Activate()

    $Caption = "Simple Example"

        $categories = @()
        $values = @()
        $chart = new-object -com OWC11.ChartSpace.11
        $chart.Clear()
        $c = $chart.charts.Add(0)
        $c.Type = 58
          $c.HasTitle = "True"
        $c.HasLegend = "True"

       $series = ([array] $chart.charts)[0].SeriesCollection.Add(0)
            
        Get-Process | foreach-object {
        $categories += $_.Name
        $values += $_.CPU * 1   
        }
           
        $series.Caption = $Caption
        $series.SetData(1, -1, $categories)
        $series.SetData(2, -1, $values)
        $filename = (resolve-path .).Path + "\jim.jpg"
        $chart.ExportPicture($filename, "jpg", 800, 500)
       
        $objSelection = $msWord.Selection
        $msword.Selection.EndKey(6)
        $objSelection.TypeParagraph()
        $msWord.Application.Selection.InlineShapes.AddPicture($filename) > Null

    EBGreenUser is Offline
    Veteran Member
    Veteran Member
    Posts:1276
    Avatar

    --
    20 Jan 2009 06:34 AM
    Hmmm...that script ran fine for me. What error did you get?
    "Look Ma...no strings!"
    P PrattUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    25 May 2010 11:47 AM
    I was getting this same error. From the advice on this thread, I went back and looked at my code. I was misspelling the object holding the method. $mangasession.getlist() should have been $mangsession.getlist()

    Thanks for the help.
    Abercrombie_LVUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    07 Jul 2010 10:55 AM
    What about this?

    function My-Custom-Function
    {
    Write-Host "Hello"
    }

    $psISE.CustomMenu.Submenus.Add("Run Custom Function",{My-Custom-Function},"Shift+Ctrl+f")

    I'm new but I can't for the life of me figure out what I'm missing.
    ChrisUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    23 Jul 2010 10:32 AM
    Abercrombie,
    The newest version of PowerShell changes a bit for the custom menus. Here's the correct code:

    function My-Custom-Function
    {
    Write-Host "Hello"
    }

    $psISE.CurrentPowerShellTab.AddonsMenu.Submenus.Add("Run Custom Function",{My-Custom-Function}, "Shift+Ctrl+f")
    vikky2266User is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    29 Sep 2011 06:35 AM
    # Add SharePoint PowerShell Snapin
    #Add-PSSnapin Microsoft.SharePoint.PowerShell
    # File and Drectory Location
    $date = get-date -Format yyyyMMdd
    $dirLocation = "\\server\share\Taxonomy\Terms\"
    New-Item ($dirLocation + $date) -type directory | Out-Null
    # Connect to Central Admininistration Site
    $taxonomySite = Get-SPSite http://ussecavdspdwk27:2010/
    # Connect to Term Store in the Managed Metadata Service Application
    $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
    $termStore = $taxonomySession.TermStores["Managed Metadata Service (Enterprise)"]
    # Connect to the Profiles Term Group
    $termStoreGroup = $termStore.Groups["EY Terms"]
    # Export Terms as .csv for each Term Set
    foreach ($termSet in $termStoreGroup.TermSets.GetEnumerator())
    {
    $termSet.Terms | select Name, Owner, CreatedDate, LastModifiedDate | Export-Csv
    ($dirLocation + $date + "\Terms-" + $termStore.Groups["Profiles"].Name + "-" + $termSet.Name + ".csv")
    } # Connect to the System Term Group
    $termStoreGroup = $termStore.Groups["System"]
    # Export Terms as .csv for each Term Set
    foreach ($termSet in $termStoreGroup.TermSets.GetEnumerator())
    {
    $termSet.Terms | select Name, Owner, CreatedDate, LastModifiedDate | Export-Csv
    ($dirLocation + $date + "\Terms-" + $termStore.Groups["System"].Name + "-" + $termSet.Name + ".csv")
    }

     
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    29 Sep 2011 06:51 AM
    Vikky: Is there a problem?
    vikky2266User is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    11 Oct 2011 06:31 AM
    Hi i need to create a bulk number of local users given in an xml file using powershell scripting ..The local users are like more than 250 to be crewated using powershell scripting with given xml file as source with all details like firsnt name,last name,job description..etc with 10 fields in it can you please help me out with some code thanks in advance ..
    blueUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    10 Jan 2012 03:01 AM
    23 Jul 2010 11:32 AM Quote Reply Alert
    Abercrombie,
    The newest version of PowerShell changes a bit for the custom menus. Here's the correct code:

    function My-Custom-Function
    {
    Write-Host "Hello"
    }

    ==============================================

    Thank you so much :) it worked with me

    $psISE.CurrentPowerShellTab.AddonsMenu.Submenus.Add("Run Custom Function",{My-Custom-Function}, "Shift+Ctrl+f")

    Sharp KidUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    03 Apr 2012 12:51 AM
    Morning all.
    First post.
    Go easy.

    I'm also getting the "You cannot call a method on a null-valued expression" error on the following bit of script:

    $Dir = get-childitem $SeqFolder\*.tps
    $List = $Dir | foreach {rename-item $_ $_.name.toupper () | write-host $_}

    It's the toupper that's the problem I think, but being a powershell numpty, I have no idea what's going on.
    If it helps, it's doing something with a csv file.
    And I'm confident that all my environment variables are correct as I've checked and double checked them.

    Totally clueless.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer