开发者

Help `-': nil can't be coerced into Fixnum (TypeError) [duplicate]

This question already has an answer here: Closed 12 years ago.

Possible Duplicate:

`-': nil can’t be coerced into Fixnum (TypeError)

Does anyone know how come guest.arrived can be nil?! This is a small part of my checkout method I have in my program. The error points at line 91 in the code which is the last part of this block of code:

departureDate = ge开发者_高级运维ts.chomp.to_i
guest.departure = departureDate
guestStayedDays = departureDate - guest.arrived # Days the guest have stayed 

I cant seem to figure this out. Is the problem with get.chomp.to_i that it is returning nil? Or is it returning 0? But then again that would not have given me this error. So is it guest that is nil or is it actually the combination guest.arrived that is the problem? Worth mentioning is that arrived is declared in my checkin method where the guest states its arrival date. Is it somehow lost?

Thanks for the answers I will post the checkin method aswell:

def self.check_in
    puts "Welcome to the checkin"
    puts "Please state your first name: "
    firstName = gets.chomp
    puts "Please state your last name:"
    lastName = gets.chomp
    puts "Write your address: "
    address = gets.chomp
    puts "and your phone number: "
    phone = gets.chomp
    puts "finally, your arrival date!"
    arrived = gets.chomp.to_i
    newPLot = $camping.generateParkingLot
    guest = Guest.new(firstName, lastName, address, phone, arrived)
    $camping.current_guests[newPLot-1] = guest
    $camping.all_guests.push(guest)   # lägg till gästen i historiken         

    puts "The registration was a success!! You have received plot " + newPLot.to_s + "."    
  end 

Here the arrived variable is defined and when I in my checkout method do puts $camping.current_guests i see all the info. Thanks!


You already asked this exact same question yesterday and the answer is still the exact same answer: the problem is that the arrived method of guest returns nil and unless you actually show the code of that method, there is nothing anyone can do about it.


Your problem is that guest.arrived is returning nil.

  • If guest was the nil, then you'd have an error on line 90 saying

    NoMethodError: undefined method `departure=' for nil:NilClass
    
  • If departureDate was nil then you'd get an error on line 91 saying

    NoMethodError: undefined method `-' for nil:NilClass
    

    Besides that, the to_i function used on the first line always returns a number -- even if you got nil or "" from somewere else, nil.to_i and "".to_i both return zero.

  • If the guest.arrived didn't exist, you'd also get a NoMethodError.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜