Error - "`+': can't convert nil into String"

Discussion in 'Ruby' started by Paul Roche, Sep 29, 2010.

  1. Paul Roche

    Paul Roche Guest

    Why does the code below give me this error? What can I do to fix it?

    /people3.rb:4:in `+': can't convert nil into String (TypeError)
    om C:/Ruby192/people3.rb:4:in `to_s'

    class Person
    attr_accessor :fname, :lname, :age
    def to_s
    "hi " + @fname + ", " + @lname
    end

    end



    Thanks
    PR
    --
    Posted via http://www.ruby-forum.com/.
     
    Paul Roche, Sep 29, 2010
    #1
    1. Advertising

  2. Paul Roche

    Luis Lavena Guest

    On Sep 29, 12:03 am, Paul Roche <> wrote:
    > Why does the code below give me this error? What can I do to fix it?
    >
    > /people3.rb:4:in `+': can't convert nil into String (TypeError)
    > om C:/Ruby192/people3.rb:4:in `to_s'
    >
    > class Person
    > attr_accessor :fname, :lname, :age
    > def to_s
    > "hi " + @fname + ", " + @lname
    > end
    >
    > end
    >


    Either @fname or @lname are nil

    You can't concatenate nil to an string.

    --
    Luis Lavena
     
    Luis Lavena, Sep 29, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,748
  2. JavaQueries
    Replies:
    1
    Views:
    3,767
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,198
  4. Bishop
    Replies:
    1
    Views:
    826
    Bishop
    Feb 24, 2007
  5. aidy
    Replies:
    8
    Views:
    384
    Gregor Kopp
    May 5, 2006
Loading...

Share This Page