在應用程序中有2頁,CompletePoll.aspx,Default.aspx。如何避免TypeInitializer異常?
CompletePoll.aspx - >的Page_Load()
Ultoo u = new Ultoo();
u.UserName = Request["username"].ToString();
u.Password = Request["password"].ToString();
new Thread(u.CompletePoll).Start();
CompletePoll()
.......
.......
String str = "Question:" + QuestionGenerator.GetNextQuestion(); /*Here i am getting Type initializer exception*/
.......
.......
QuestionGenerator
public static class QuestionGenerator
{
private static string[] FirstParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")).ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
private static string[] SecondParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart2.txt")).ReadToEnd(). Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
private static Random r = new Random();
public static string GetNextQuestion()
{
return FirstParts[r.Next(0, FirstParts.Length - 1)] + " " + SecondParts[r.Next(0, SecondParts.Length - 1)] + "?";
}
}
但是,如果我先調用Default.aspx,然後CompletePoll.aspx代碼工作正常。
的Default.aspx - >的Page_Load()
Label1.Text = QuestionGenerator.GetNextQuestion();
所以在這裏我的問題是,如果我訪問CompletePoll.aspx 第一我收到TypeInitializer例外。如果我先訪問Default.aspx,然後CompletePoll.aspx,我沒有得到任何問題。我的代碼有什麼問題,我錯過了什麼?我如何首先訪問CompletePoll.aspx?
什麼是內部異常? – akatakritos
InnerException:System.NullReferenceException HResult = -2147467261 Message =未將對象引用設置爲對象的實例。來源= WebTracing StackTrace:在UltooAgent.QuestionGenerator..cctor()在c:\ Users \ Rajeev \ Desktop \ Tracing \ Tracing \ Tracing \ WebTracing \ WebTracing \ QuestionGe nerator.cs:line 12 – Rajeev