如何修复未在C#代码中设置对象实例的Object引用

时间:2013-10-30 16:02:56

标签: c#

private void label16_MouseEnter(object sender, EventArgs e)
{
    panelSummary.Show();
    StockInfo temp = Game.getStockByLabelName(label16.Text);
   // StockComp.Text = "Stock Company = " + temp.getStockName();// I got errors in this line
    lbDispStockPrice.Text = "Stock price = " + temp.getPrice();
    lbDispStock.Text = "Stock category = " + temp.getCategory();
    owner.Text = "Property Of " + temp.getBuyer();
}

2 个答案:

答案 0 :(得分:1)

要在您指定的行上“停止例外”,请检查null

返回的GetStockByLabelName
StockInfo temp = Game.getStockByLabelName(label16.Text);

if(temp != null)
{
     StockComp.Text = "Stock Company = " + temp.getStockName();//if temp is null then EX
     //...
}
else
       //do whatever is appropriate for the situation where StockInfo is null

StockComp.Text可能导致错误。我认为这是设计师创建的TextBox,不太可能。

答案 1 :(得分:0)

确保这些引用中的每一个都不是null

panelSummary
temp 
lbDispStockPrice
lbDispStock
owner

我的猜测是temp,因为其他似乎是视觉组件,这意味着Game.getStockByLabelName在传递null中的值时会返回label16.Text

这是调试器派上用场的地方......