2013-10-30 11 views
0
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(); 
} 
+4

大概'getStockByLabelName'已經返回null ...你需要找出原因。 –

+0

當這不是C時,爲什麼你在這裏有一個「c」標籤,並且哪一行會拋出錯誤?您正在引用的對象之一(panelSummary,Game,temp等)爲空。你是否通過調試器來查看哪一個? – OldProgrammer

+0

我接下來的建議是啓動一個調試器來查看語句中哪個變量出錯是'null'。 – millimoose

回答

0

確保每個這些引用的不是null

panelSummary 
temp 
lbDispStockPrice 
lbDispStock 
owner 

我的猜測是因爲別人temp似乎是可視化組件,這將意味着Game.getStockByLabelName在返回null時通過了label16.Text中的值。

這是調試器就派上用場了......

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,所以不太可能。

相關問題