rubymotion-community / BubbleWrap

Cocoa wrappers and helpers for RubyMotion (Ruby for iOS and OS X) - Making Cocoa APIs more Ruby like, one API at a time. Fork away and send your pull requests
Other
1.18k stars 208 forks source link

undefined method `latitude' for nil:NilClass (NoMethodError) #401

Closed andersennl closed 10 years ago

andersennl commented 10 years ago

I've just added BubbleWrap to my project and getting the error from the title. This is my rakefile:

# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'
require "rubygems"
require 'bundler'
require 'bubble-wrap/location'
Bundler.require

Motion::Project::App.setup do |app|
  # Use `rake config' to see complete project settings.
  app.name = 'myapp'
end

This is the gem version: gem "bubble-wrap", "~> 1.5.0". And this is how I update the location:

  def update_location
    BW::Location.get(purpose: 'We need to use your GPS because...') do |result|
      p "From Lat #{result[:from].latitude}, Long #{result[:from].longitude}"
      p "To Lat #{result[:to].latitude}, Long #{result[:to].longitude}"
    end
  end

Here is the full error:

`block in update_location': undefined method `latitude' for nil:NilClass (NoMethodError)
    from location.rb:120:in `locationManager:didUpdateToLocation:fromLocation:'
2014-07-23 22:50:44.865 myproject[12130:70b] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'map_screen.rb:48:in `block in update_location': undefined method `latitude' for nil:NilClass (NoMethodError)
    from location.rb:120:in `locationManager:didUpdateToLocation:fromLocation:'
'
*** First throw call stack:
(
    0   CoreFoundation                      0x02bfe1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00cbf8e5 objc_exception_throw + 44
    2   myproject                           0x00864e6a _ZL10__vm_raisev + 346
    3   myproject                           0x00864f48 rb_vm_raise + 216
    4   myproject                           0x00767751 rb_exc_raise + 17
    5   myproject                           0x00862fbe rb_vm_method_missing + 574
    6   myproject                           0x00836bbf rb_method_missing + 31
    7   myproject                           0x0083fd2e rb_vm_dispatch + 5038
    8   myproject                           0x0083e81c rb_vm_trigger_method_missing + 1004
    9   myproject                           0x0083f6eb rb_vm_dispatch + 3435
    10  myproject                           0x006e97ac vm_dispatch + 1100
    11  myproject                           0x006f1e65 rb_scope__update_location__block__ + 181
    12  myproject                           0x0084e2cb _ZL20dispatch_bimp_callerPFP11objc_objectS0_P13objc_selectorzEmS1_mP11rb_vm_blockiPKm + 46507
    13  myproject                           0x008416e1 _ZL13vm_block_evalP7RoxorVMP11rb_vm_blockP13objc_selectormiPKm + 1137
    14  myproject                           0x00841248 rb_vm_block_eval + 104
    15  myproject                           0x007725ef proc_call + 31
    16  myproject                           0x0083fd2e rb_vm_dispatch + 5038
    17  myproject                           0x000df73c vm_dispatch + 1100
    18  myproject                           0x000ea012 rb_scope__locationManager:didUpdateToLocation:fromLocation:__ + 946
    19  myproject                           0x000ea0ec __unnamed_69 + 108
    20  CoreLocation                        0x0165455e CLLocationCoordinate2DGetDistanceFrom + 18294
    21  CoreLocation                        0x01653af4 CLLocationCoordinate2DGetDistanceFrom + 15628
    22  CoreLocation                        0x01650e40 CLLocationCoordinate2DGetDistanceFrom + 4184
    23  CoreLocation                        0x0164a680 CLClientInvalidate + 996
    24  CoreFoundation                      0x02bbd0b0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
    25  CoreFoundation                      0x02b86339 __CFRunLoopDoBlocks + 361
    26  CoreFoundation                      0x02ba4753 __CFRunLoopRun + 2355
    27  CoreFoundation                      0x02ba39d3 CFRunLoopRunSpecific + 467
    28  CoreFoundation                      0x02ba37eb CFRunLoopRunInMode + 123
    29  GraphicsServices                    0x048ec5ee GSEventRunModal + 192
    30  GraphicsServices                    0x048ec42b GSEventRun + 104
    31  UIKit                               0x01c62f9b UIApplicationMain + 1225
    32  myproject                           0x0002918c main + 156
    33  libdyld.dylib                       0x04200701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NoMethodError

What am I doing wrong? Thanks!

clayallsopp commented 10 years ago

Did you try to examine what result was? ie puts result?

clayallsopp commented 10 years ago

& can you also try updating your BubbleWrap version? gem "bubble-wrap", "~> 1.7.0" (I've updated the README accordingly)

andersennl commented 10 years ago

I've updated the gem version. This is the output of p result:

{:to=>#<CLLocation:0xafa95c0>, :from=>nil}
{:to=>#<CLLocation:0xab525d0>, :from=>#<CLLocation:0xa9c2de0>}
{:to=>#<CLLocation:0xaf72690>, :from=>#<CLLocation:0xaf072e0>}
{:to=>#<CLLocation:0x1f5edf10>, :from=>#<CLLocation:0x1f56af10>}
{:to=>#<CLLocation:0xc4702f0>, :from=>#<CLLocation:0xc447fc0>}
{:to=>#<CLLocation:0xab480e0>, :from=>#<CLLocation:0xab53530>}

As I just pasted the result I realize that the second CLLocation on the first line is nil. This is the problem, right? I should check if it's nil? Thanks for your help!

Update Ok, I've refactored the code a little bit. If I do it like the following it works perfectly:

      p "From Lat #{result[:from].latitude}" if result[:from]
      p "Long #{result[:from].longitude}" if result[:from]
      p "To Lat #{result[:to].latitude}" if result[:to]
      p "Long #{result[:to].longitude}" if result[:to]
clayallsopp commented 10 years ago

Yup, exactly - from Apple's docs: "The location data from the previous update. If this is the first update event delivered by this location manager, this parameter is nil."