Discussion:
[問題] 並未將物件參考設定為物件的執行個體
(时间太久无法回复)
等比
2010-02-10 04:28:33 UTC
Permalink
碰到的問題挺難確定要去DB還是網設版問XD

不過用的是C#就先來問問看囉

這是一個ASP.NET網頁的其中一段




void Page_Load(object sender, EventArgs e)

{
string dbString = "Provider=Microsoft.Jet.OleDb.4.0;
Data Source=" + Server.MapPath("db8_1.mdb");
OleDbConnection dbConn = new OleDbConnection(dbString);
dbConn.Open();
string cmdString = "SELECT * FROM data";
OleDbCommand dbCommand = new OleDbCommand(cmdString, dbConn);
OleDbDataReader myReader = dbCommand.ExecuteReader();

string[ , ] Myarr = new string[20, 7] ;
int i = 0;
while (myReader.Read( ))
{
Myarr[i, 0] = (string)myReader["Question"];
Myarr[i, 1] = (string)myReader["A"];
Myarr[i, 2] = (string)myReader["B"];
Myarr[i, 3] = (string)myReader["C"];
Myarr[i, 4] = (string)myReader["D"];
Myarr[i, 5] = (string)myReader["E"];
Myarr[i, 6] = (string)myReader["Answer"];
i++;
}

//到此程式執行上都沒有發生問題

//有使用非以下的方法測試了前幾個陣列和
Label還有RadioButtonList之間的繫結,也是ok的

//但是換成了以下的方法卻又發生了錯誤

Label L = new Label( );
RadioButtonList RBL = new RadioButtonList( );
for (int n = 1; n <= 10; n++)
{
L = (Label)FindControl("L" + n);
RBL = (RadioButtonList)FindControl("RBL" + n);

L.Text = (string)Myarr[n-1, 0];
//執行時發生錯誤的地方。
"並未將物件參考設定為物件的執行個體"

//在學期中是採用L.Text = (string)myReader.Read["Question"];
並沒有此問題的產生

//不過這次需求不太一樣,換從陣列讀取值,但是問題似乎又不在陣列上
難道是L在宣告上有什麼錯誤發生嗎?

RBL.Items[0].Text = (string)Myarr[n-1, 1];
RBL.Items[1].Text = (string)Myarr[n-1, 2];
RBL.Items[2].Text = (string)Myarr[n-1, 3];
RBL.Items[3].Text = (string)Myarr[n-1, 4];
RBL.Items[4].Text = (string)Myarr[n-1, 5];
RBL.Items[0].Value = "A";
RBL.Items[1].Value = "B";
RBL.Items[2].Value = "C";
RBL.Items[3].Value = "D";
RBL.Items[4].Value = "E";
}

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.143.23.190
忘了補發生什麼事
※ 編輯: denby 來自: 220.143.23.190 (02/10 12:28)
Optimist
2010-02-10 10:06:13 UTC
Permalink
→ denby:用逐步執行 找到L是null 但是,不是有宣告了嗎?



Label L;
^^^^^^^^
這只是宣告一個欄位: Type是Label,名字叫做L。

Label L = new Label();
^^^^^^^^^^^^^^^^^^^^^^
宣告一個Type是Label變數,名字叫做L,然後我new了一個Label的執行個體,並且存進去



L = null;
^^^^^^^^^
變數L裡面沒東西,當然他就不知道要去存取誰的Text。

new關鍵字
http://msdn.microsoft.com/zh-tw/library/51y09td4.aspx

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.30.251

Loading...