none
Cannot Call a Method on a Null-Valued Expression

    Question

  • I am currently getting the following error in a script I am trying to run to learn the basics of Powershell. Any assistance would be most appreciated.


    Error:
    You cannot call a method on a null-valued expression.
    At C:\shellscripts\chapter3.ps1:35 char:25
    + $answer = $randomNo.Next <<<< (1,5)
        + CategoryInfo          : InvalidOperation: (Next:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    The script is as follows:

    =====

    Clear-host

    write-host $(“`n” * 8)

    write-host “ The Powershell Fortune Teller “

    write-host $(“`n” * 5)

    write-host “ =_= “

    write-host $(“`n” * 5)

    write-host “ Press enter to continue. "

    read-host

    #...

    #...

    clear-host

    write-host $(“`n” * 3)

    write-host “ This script is limited to answering only yes/no questions. “

    write-host $(“`n” * 5)

    write-host “ Please phrase questions appropriately. =( “

    write-host $(“`n” *4)

    write-host “ Press enter to continue. ”

    read-host

    #...

    #...

    #Continue gameplay until the player decides to stop

    while ($status -ne “stop”) {

     

    #Ask the player the first question

    while ($question -eq “”) {

    clear-host

    write-host

    $question = read-host “ What is your question? “

    }

     

    $question = “” 

    #Reset variable to an empty string

     

    #Retrieve a random number between 1 and 4

    $answer = $randomNo.Next(1,5)

     

    #Select an answer based on the time and the random number

    if ($time -gt 12) {

    write-host

    if ($answer -eq 1) { “The answer is no.” }

    if ($answer -eq 2) { “The answer is never.” }

    if ($answer -eq 3) { “The answer is unclear.” }

    if ($answer -eq 4) { “The answer is yes.” }

    }

    #...

    #...

    else {

    write-host

    if ($answer -eq 1) { “The answer is yes.” }

    if ($answer -eq 2) { “The answer is always.” }

    if ($answer -eq 3) { “The answer is uncertain.” }

    if ($answer -eq 4) { “The answer is no.” }

    }

    #...

    #...

    write-host $(“`n” * 8)

    write-host “ Press enter to continue. “

    read-host

    clear-host

    write-host

    $reply = read-host “ Press enter to ask another question or type Q to quit. “ 

    if ($reply -eq “q”) { $status = “Stop” }

    }

    Tuesday, September 13, 2011 5:11 PM

Answers

  • Where you define variable $randomNo?

    $answer = $randomNo.Next(1,5) replace to $answer = Get-Random -Minimum 1 -Maximum 5

    • Edited by KazunMVP Tuesday, September 13, 2011 5:14 PM
    • Proposed as answer by MrWasabi1 Wednesday, September 14, 2011 1:30 AM
    • Marked as answer by Tiger Li Monday, September 19, 2011 2:54 AM
    Tuesday, September 13, 2011 5:13 PM
  • I didn't use any books to learn Powershell.  I started by watching a 5-part series by Powershell guru, Ed Wilson, which you can see here:

    http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

    Thats about 5 hours of excellent and free education!

    Then, I started trying to do things, and asking questions on this forum.  I soon learned that you can learn a lot about Powershell from Powershell itself.  The built-in help is superb.

    For example, type:

    Get-Help *about*

    ...and you will see all the 'about' topics.  Read through those as needs be. 

    Also, for each cmdlet, there are examples.  Try them out, play with them, change them, and just generally have fun learning this wonderful language!

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Tiger Li Monday, September 19, 2011 2:54 AM
    Wednesday, September 14, 2011 2:42 PM
  • I've been pretty pleased with www.powershell.com's ebook...
     
    I typically just attempt little tasks and figure it out as I go and then
    work mostly with technet and msdn for references..
     
    but, I have a decent amount of programming knowledge, so, perhaps that
    method may not work too well depending on your experience with
    coding/scripting.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tiger Li Monday, September 19, 2011 2:54 AM
    Wednesday, September 14, 2011 3:37 PM

All replies

  • Where you define variable $randomNo?

    $answer = $randomNo.Next(1,5) replace to $answer = Get-Random -Minimum 1 -Maximum 5

    • Edited by KazunMVP Tuesday, September 13, 2011 5:14 PM
    • Proposed as answer by MrWasabi1 Wednesday, September 14, 2011 1:30 AM
    • Marked as answer by Tiger Li Monday, September 19, 2011 2:54 AM
    Tuesday, September 13, 2011 5:13 PM
  • I really just have to say this, newbtopowershell:

    One reason why you may be having trouble debugging your code is because it is very badly laid out.  If this is how you are learning to write Powershell, you are definitely learning from the wrong book.

    This is not BASIC, it's Powershell, and we don't write it like that, normally.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, September 13, 2011 5:42 PM
  • To be honest, I copied the code from a book I am using almost verbatim. However, the quality of its contents does seem questionable since I keep finding these errors. 

     

    Is there a specific book that you would recommend? 

    Wednesday, September 14, 2011 2:20 PM
  • I didn't use any books to learn Powershell.  I started by watching a 5-part series by Powershell guru, Ed Wilson, which you can see here:

    http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

    Thats about 5 hours of excellent and free education!

    Then, I started trying to do things, and asking questions on this forum.  I soon learned that you can learn a lot about Powershell from Powershell itself.  The built-in help is superb.

    For example, type:

    Get-Help *about*

    ...and you will see all the 'about' topics.  Read through those as needs be. 

    Also, for each cmdlet, there are examples.  Try them out, play with them, change them, and just generally have fun learning this wonderful language!

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Tiger Li Monday, September 19, 2011 2:54 AM
    Wednesday, September 14, 2011 2:42 PM
  • Also on this same page:

    http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

    You can see what books are recommended.  They are all by Ed Wilson anyway!


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, September 14, 2011 2:52 PM
  • I've been pretty pleased with www.powershell.com's ebook...
     
    I typically just attempt little tasks and figure it out as I go and then
    work mostly with technet and msdn for references..
     
    but, I have a decent amount of programming knowledge, so, perhaps that
    method may not work too well depending on your experience with
    coding/scripting.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tiger Li Monday, September 19, 2011 2:54 AM
    Wednesday, September 14, 2011 3:37 PM