Forum: Ruby Undefined method '[]' I dont know why.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
60789b6177d72d0ed6a810ad82bc02ac?d=identicon&s=25 Mateusz Ga (sobcios)
on 2008-11-03 20:18
Hello , I have problem with my script.

/////////////this function return true or false
  def moznazbudowac?(co,sesja,stan)
               budynek = ""
                catch(:redo) do
                        case co
                        when 1
                                budynek = "Kopalnia metalu"
                        when 2
                                budynek = "Kopalnia krysztalu"
                        when 3
                                budynek = "Kopalnia deuteru"
                        end
                end
                        poziom = stan[budynek]  # 11
                        poziom = poziom.to_i
                        surowce_pods = bazadanych(co) #output
[60,15,0,1.5]
                        surowce_potrz = [0,0,0,0]
surowce_potrz[0] = surowce_pods[0].to_i
*surowce_pods[3].to_i**(poziom-1)#Error
                 [.....]# further part of script
//////////////////////


Error : Undefined method '[]' for nil:NilClass <NoMethodError>
I try to modify script but dont know how fix this error.

a = moznazbudowac?(1,sesja,stan) // this is working

But when i execute this
sesja = sesja()
stan = skaner ()
kolejka() return array i was testing with array  [1]
test_kolejki = kolejka()
kolejkowanie(test_kolejki,sesja,stan)#error
I have error in function kolejkowanie() and i line above and in function
moznazbudowac?.

/////////////////
def kolejkowanie(kolejka,sesja,stan)
  dlugosc = kolejka.length
  dlugosc -=1
  nr = 0
  while nr<=dlugosc
    zbudowano = 0
    while zbudowano == 0
       if moznazbudowac?(kolejka[nr],sesja,stan) == true then#error
        zbuduj(kolejka[nr],sesja)
        kolejka.pop
        nr +=1
        zbudowano = 1
       else
        "Nie starczy surowcow"
       end
    end

  end

end
///////////////////
48aef6c74ff21bfe894b201c7936fcdd?d=identicon&s=25 Nation, Carey (Guest)
on 2008-11-03 20:38
(Received via mailing list)
Hi,
Well, it's really telling you that something that you're indexing into
is nil.  Not sure which of the things in that big assignment is the nil
one, but something there is.

HTH,
Carey
60789b6177d72d0ed6a810ad82bc02ac?d=identicon&s=25 Mateusz Ga (sobcios)
on 2008-11-03 21:00
Thx for help. Now i see what's wrong:
 if moznazbudowac?(kolejka[nr],sesja,stan) == true

kolejka does error. I will fix this tomorrow.
This topic is locked and can not be replied to.