F#中的NullReferenceException [英] NullReferenceException in F#

查看:85
本文介绍了F#中的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我逐步执行以下代码时,第二行上的report为空. 但是,第三行会生成NullReferenceException.

When I step through the following code, report on the second line is null. However, the third line generates a NullReferenceException.

member this.setTaggedResearchReportList (index : int) (taggedResearchReport : TaggedResearchReportUIVO option) =
    let report = Option.get(taggedResearchReport)
    if not(report.Equals(null)) then
        // do some stuff here

那是为什么,我该怎么做才能避免呢?谢谢!

Why is that, and what can I do to avoid it? Thanks!

这是调用this.setTaggedResearchReportList的行:

getMostRecentTaggedResearchReportForSecurityId (item.id) (new Action<_>(this.setTaggedResearchReportList 0))

这是getMostRecentTaggedResearchReportForSecurityId方法:

let getMostRecentTaggedResearchReportForSecurityId (securityId : int) (callbackUI : Action<_>) =
    getSingleRPCResult<JSONSingleResult<TaggedResearchReportUIVO>, TaggedResearchReportUIVO>
        "TaggedResearchReportRPC"  
        "getMostRecentResearchReportForSecurityId" 
        (sprintf "%i" securityId)
        callbackUI
        (fun (x : option<JSONSingleResult<TaggedResearchReportUIVO>>) ->
            match x.IsSome with
                | true -> Some(Option.get(x).result)
                | false -> None 
        )

推荐答案

您的TaggedResearchReportUIVO类型显然是在F#中定义的,不允许null作为适当的值.因此,编译器将阻止您将文字null用作该类型的值.但是,其他一些代码却在编译器后面,并在其中保留了空值.要变通解决当前问题,您可以尝试与Unchecked.defaultof<TaggedResearchReportUIVO>而不是null进行比较.

Your TaggedResearchReportUIVO type is evidently defined in F#, and doesn't allow null as a proper value. Therefore, the compiler will prevent you from using the literal null as a value of that type; however, some other code is going behind the compiler's back and sticking a null value in there. To work around the immediate issue, you can try comparing against Unchecked.defaultof<TaggedResearchReportUIVO> instead of null.

但是,也许值得评估一下您是否应该进行一些更重要的更改来避免此类问题.例如,如果将null作为类型TaggedResearchReportUIVO的适当值有意义,则可以将[<AllowNullLiteral>]属性添加到该类型的定义中.另外,如果确实没有必要使用null作为适当的值,那么您需要研究生成有问题的值的代码.

However, it's probably worth assessing whether you should be making some more significant changes to avoid this type of issue in the first place. For instance, if it makes sense to have null as a proper value of type TaggedResearchReportUIVO, then you could add the [<AllowNullLiteral>] attribute to the definition of that type. Alternatively, if it really doesn't make sense to use null as a proper value, then you need to investigate the code which is generating the problematic value.

顺便说一句,您代码的其他部分可以大量清理.例如,考虑更改

As an aside, there are other parts of your code that can be cleaned up considerably. For example, consider changing

fun (x : option<JSONSingleResult<TaggedResearchReportUIVO>>) -> 
    match x.IsSome with
    | true -> Some(Option.get(x).result)
    | false -> None

Option.map (fun (jsr : JSONSingleResult<_>) -> jsr.result)

这篇关于F#中的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆