rubymethodsnull

NoMethodError Occurred, undefined method for nil:NilClass


First of all, I know it has been posted, I've seen most of the question posts but I still don't understand how it works.

So I'm getting the error:

Script 'Terrain Tag Detection ~' line 115: NoMethodError occurred.

undefined method '[]' for nil:NilClass

My 'Terrain Tag Detection ~' Script looks like this: http://pastebin.com/PUypTwJs (can't paste the code here correctly, and yes it's about Pokemon).


Solution

  • That means your @map = WildPokemon.fetch($game_map.map_id) method doesn't return and value and you want to access @map variable.

    You can add a check in your code like below:

    @map = WildPokemon.fetch($game_map.map_id)
    if @map.present?
        @enemy = @map[0][rand(@map[0].size)]
        @level = @map[1][rand(@map[1].size)]
    end