2016-11-15 76 views
-2

我在这条线的错误说“未将对象引用设置到对象的实例”对象引用未设置到对象

Me.ShipTextValue.Text = IIf(String.IsNullOrWhiteSpace(orderHeader.ShippingText), String.Empty, orderHeader.ShippingText.Replace(Environment.NewLine, "<br />")) 

的实例,但是,我不认为有一个在这一行的错误,任何人都可以帮助我看看是否有任何错误在该行?

非常感谢

+0

可能重复[什么是NullReferenceException,以及如何解决它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix -it) –

+0

尝试'If'而不是'IIf'。查看[this](http://stackoverflow.com/questions/28377/performance-difference-between-iif-and-if)以获取更多关于差异的信息。 – Bugs

回答

2

不要使用旧VB6 IIF functionIf-operator,它使用而不是IIF这evaulates两个表达式即使第一已是True短路评价。

这会导致NullReferencexception如果orderHeader.ShippingTextNothing

Me.ShipTextValue.Text = If(String.IsNullOrWhiteSpace(orderHeader.ShippingText), String.Empty, orderHeader.ShippingText.Replace(Environment.NewLine, "<br />")) 

如果您使用Visual Basic 14可以使用null propagation operator

ShipTextValue.Text = orderHeader.ShippingText?.Replace(Environment.NewLine, "<br />") 

这也将返回""如果orderHeader.ShippingTextNothing