Runtime NullReferenceException when nil checking an int out of a dictionary


(Jon Nermut) #1

This swift Echoes code in Fire 2321:

print("The magic happens here.")

let dict = Dictionary<String, AnyObject>()
dict["number"] = 4
let myNum: AnyObject? = dict["number"]
if myNum != nil
{
    print(myNum)
}

Gives at runtime:

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,object)
  at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet] (System.Runtime.CompilerServices.CallSite site, System.Dynamic.T0 arg0, System.Dynamic.T1 arg1) [0x00132] in /private/tmp/source-mono-4.3.2/bockbuild-xamarin/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/UpdateDelegates.Generated.cs:387 
  at EchoesConsoleApplication7.__Global.Main (System.String[] __args) [0x0009a] in /Users/jon/Documents/EchoesConsoleApplication7/Program.swift:9 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,object)
  at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet] (System.Runtime.CompilerServices.CallSite site, System.Dynamic.T0 arg0, System.Dynamic.T1 arg1) [0x00132] in /private/tmp/source-mono-4.3.2/bockbuild-xamarin/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/UpdateDelegates.Generated.cs:387 
  at EchoesConsoleApplication7.__Global.Main (System.String[] __args) [0x0009a] in /Users/jon/Documents/EchoesConsoleApplication7/Program.swift:9 

Ironically, the NRE is thrown from the nil check. Take it out, and it prints 4. I guess its somehow because myNum is assigned a System.Int64 which isnt nullable?

EchoesConsoleApplication7.zip (263.9 KB)


(RemObjects) #2

Thanks, logged as bugs://80935


(RemObjects) #3

bugs://80935 got closed with status fixed.