首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Xamarin上使用F#和iOS的System.NullReferenceException

在Xamarin上使用F#和iOS的System.NullReferenceException
EN

Stack Overflow用户
提问于 2015-02-16 21:24:57
回答 1查看 192关注 0票数 0

当我执行一些测试代码来测试不同的模块时,我遇到了一个非常奇怪的错误,这个模块托管在一个独立的项目中,但是在Xamarin Studio的相同解决方案中。

该解决方案承载2个项目。一个模块项目和一个测试项目。测试项目通过project-reference引用module-project。

代码语言:javascript
复制
//module-project/FluidLayout.fs
namespace iosautolayout
open System

 module Fluidity =
     type Init<'a> = Init of 'a
     type Right<'a> = Right of 'a
     type Left<'a> = Left of 'a

     type HorizontalBuilder() =
         member x.Yield(()) = Init "Init"
         [<CustomOperation("part")>]
         member x.Part(Init v, a) = Left "Left"

     let horizontal = new HorizontalBuilder()

      let create () = 
          let test_h = horizontal {
              part "test"
          }
          test_h

然后我有了包含两个文件的测试项目// test - project /Tests.fs命名空间ioslibrarytests

代码语言:javascript
复制
open System
open NUnit.Framework;
open iosautolayout.Fluidity
open MonoTouch.UIKit
open MonoTouch.Foundation

#nowarn "0025"

[<TestFixture>]
type Tests() =

    [<Test>]
    member me.``Create Structure with DSL``() =
        let z = create ()
        ()

和启动一切的AppDelegate

代码语言:javascript
复制
//test-project/AppDelegate.fs
namespace ioslibrarytests

open System
open MonoTouch.UIKit
open MonoTouch.Foundation
open MonoTouch.NUnit.UI

[<Register("AppDelegate")>]
type AppDelegate() = 
    inherit UIApplicationDelegate()
    override val Window = null with get, set
    override this.FinishedLaunching(app, options) = 
        this.Window <- new UIWindow(UIScreen.MainScreen.Bounds)
        let runner = new TouchRunner(this.Window)
        runner.Add (System.Reflection.Assembly.GetExecutingAssembly ());
        this.Window.RootViewController <- new UINavigationController (runner.GetViewController ());
        this.Window.MakeKeyAndVisible()
        true

module Main = 
    [<EntryPoint>]
    let main args = 
        UIApplication.Main(args, null, "AppDelegate")
        0

这会给出以下错误

代码语言:javascript
复制
 2015-02-15 15:11:46.902 ioslibrarytests[1017:14804]    
 [FAIL] Create Structure with DSL : System.NullReferenceException : 
        Object reference not set to an instance of an object
 2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at
 iosautolayout.Fluidity.create () [0x00000] in 
 /Users/projekte/pawo-pic/ios-autolayout/FluidLayout.fs:17 
 2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at
 ioslibrarytests.Tests.Create Structure with DSL () [0x00000] in 
 /Users/projekte/pawo-pic/ios-library-tests/Tests.fs:17 
 2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at 
 (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke 
 (System.Reflection.MonoMethod,object,object[],System.Exception&)
 2015-02-15 15:11:46.902 ioslibrarytests[1017:14804] at 
 System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags 
 invokeAttr,   System.Reflection.Binder binder, System.Object[] parameters, 
 System.Globalization.CultureInfo culture) [0x00044] in 
 /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:230 201

我对这里发生的事情完全不知所措。

非常感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-02-21 15:59:04

我找到了一个解决方案--但现在我更困惑了。

如果我将horizontal改为函数,而不是val,则一切正常

代码语言:javascript
复制
namespace iosautolayout
open System

module Fluidity =
    type Init<'a> = Init of 'a
    type Right<'a> = Right of 'a
    type Left<'a> = Left of 'a

    type HorizontalBuilder() =
        member x.Yield(()) = Init "Init"
        [<CustomOperation("part")>]
        member x.Part(Init v, a) = Left "Left"

    let horizontal () = new HorizontalBuilder()
    let create () = 
        let test_h = horizontal() {
            part "test"
        }
        test_h

在交互式窗口中,无论我是将水平作为值还是作为函数使用都没有关系。

有没有人能解释一下这种行为?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28542455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档