2011-02-03 14 views
0

我在運行我的應用程序時遇到了一個奇怪的問題。asp.net中同一語句的對象引用null

我有一個類[say studentInfo],爲此我在頁面加載之上聲明瞭一個實例[即頁面範圍]

StudentInfo sInfo; 

在Page_Load中的我打電話這種情況下調用這個類的功能,一切工作正常:

例如:string studentName = sInfo.GetStudentId(studentId);

但是,當我寫相同的上述代碼在同一頁上的不同的功能,我收到此錯誤:

對象引用不能設置爲null

private infoList GetInfo() 
{ 
int studentId = // some logic; 
string studentName = sInfo.GetStudentId(studentId); 
} 
+0

你可能之前調用`Page_Load`此方法。 – Adeel 2011-02-03 09:20:38

+0

您可以粘貼多一點的代碼,例如:你在哪裏設置`sInfo`? – 2011-02-03 09:20:57

回答

2

您在聲明變量,但沒有創造它

嘗試StudentInfo sInfo = new StudentInfo();

相關問題