NullReferenceException 未处理 C# [英] NullReferenceException was unhandled C#
本文介绍了NullReferenceException 未处理 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Visual Studio 2010 - C# 中的(Windows 窗体)
Visual Studio 2010 - (Windows Forms) in C#
我有这个代码:
private void cbxValuta_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string primo = cbxValuta.SelectedItem.ToString();
string secondo = cbxValuta2.SelectedItem.ToString();
double cambio = double.Parse(CurrencyConverter.Convert(1.0m, primo, secondo));
tbxConvertito.Text = (double.Parse(tbxDaConvertire.Text) * cambio).ToString();
我收到此错误:
NullReferenceException 未处理未将对象引用设置为对象的实例.
NullReferenceException was unhandled Object reference not set to an instance of an object.
我该如何解决这个问题?
How Can I solve this issue?
推荐答案
SelectedItem
如果未在 UI 元素中选择任何项目,则返回 null.尝试添加检查项目是否已被选中
SelectedItem
return null if no item was selected in a UI element. Try to add check if items has been selected
if(cbxValuta.SelectedItem != null && cbxValuta2.SelectedItem != null)
{
string primo = cbxValuta.SelectedItem.ToString();
string secondo = cbxValuta2.SelectedItem.ToString();
// ....
}
这篇关于NullReferenceException 未处理 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文