C#异常:在检查是否为null时,无法对空引用执行运行时绑定

时间:2016-11-04 22:29:35

标签: c# exception-handling null

我只需要在实际使用它之前检查对象是否为null,但它会不断抛出此异常: 无法对空引用执行运行时绑定

# If just signed up or just logged in, go to the user's home page and not
# back to this product landing page that referred them
if auth.is_logged_in():

    if  '/default/user/login?_next=/fnd/home/info' in request.env.http_referer:
        redirect( URL( 'user', 'show_campaigns') )

    if  '/default/user/register?_next=/fnd/home/info' in request.env.http_referer:
        redirect( URL( 'user', 'show_campaigns') )


return dict()

if (interMarket.marketItems.prices != null) { // do something.. } 包含来自JSON的数据。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

抛出了什么特定的异常?你的代码在哪里抛出? interMarket或interMarket.marketItems是否可能为空?如果是这样,使用空条件运算符(?。)将解决问题:

if (interMarket?.marketItems?.prices != null) {
// do something..
}