2012-03-23 45 views
0

我已經將文本框轉換爲接受整數。但是,當我輸入一個數值到文本框中,然後單擊「查找」按鈕時,會發生錯誤(NullReferenceException),any1是否可以幫助我解決此問題?如果可能請告訴我錯誤的原因。如何處理NullReferenceException?

private void Find_Click(object sender, EventArgs e) 
{ 
    int convertedBranchID; 
    convertedBranchID = Convert.ToInt32(branchID.Text); 
    convertedBranchID = int.Parse(branchID.Text); 

    string selectDayOfWeek = dayOfWeek.Items[dayOfWeek.SelectedIndex].ToString(); 
    //dayOfWeek is the name of a combobox 


    //'NullReferenceException was unhandled' occurs here.... 
    DataRow[] findBranchID = RetailCamDataSet1.Tables["smBranchWorkingDayInfo"].Select("BranchID='" + searchBranchID + "'"); 

    branchIDResult = findBranchID.Length; 

    if (dayOfWeek.SelectedItem.ToString() == "Sunday") 
    { 

    } 
} 
+0

如何填寫RetailCamDataSet1? – 2012-03-23 05:00:37

+0

'RetailCamDataSet1'可能不包含'smBranchWorkingDayInfo'表。 – 2012-03-23 05:01:42

+0

Thx every1。剛纔我意識到我沒有填寫RetailCamDataSet1,然後我使用2個不同的DataAdapter來填充2個不同的表,然後它可以工作,但是我需要使用2個不同的DataAdapter來填充嗎?或者我可以只使用1個DataAdapter? RetailCamDataAdapter.Fill(RetailCamDataSet1,「pcPeopleCountingValue」); RetailCamDataAdapter1.Fill(RetailCamDataSet1,「smBranchWorkingDayInfo」); – hakunabean 2012-03-23 05:04:38

回答

0

RetailCamDataSet1或您正在索引的表爲空。如果RetailCamDataSet1爲null並且您調用Tables屬性,則您試圖引用null。

與選擇相同。例如,如果沒有名爲smBranchWorkingDayInfo(typo?not filled?)的表,那麼索引到該表中將返回null,因此對空對象調用Select將導致空引用異常。

在之前設置一個斷點並檢查RetailCamDataSet1對象(確保它不爲空)。你也可以嘗試在它上面添加一些迭代遍歷表的調試代碼,看看這個表是否存在。

+0

thx爲您解釋。 – hakunabean 2012-03-23 05:11:36

相關問題