2011-11-04 36 views
0

選擇選項,我遇到一個問題,而選擇在我的MessageBox 的按鈕沒有一個錯誤,同時從DialogResult的

Object reference not set to an instance of an object. 

從行:

AddEntryWindow addWindow = new AddEntryWindow 
    (this, storedAuth.UserName, storedAuth.Password); 

我不明白有什麼問題,因爲在此之後幾行字,是相同的語句。我怎樣才能解決這個問題?

固定

private void tsmiAddEntry_Click(object sender, EventArgs e) 
{ 
    if (storedAuth == null) 
    { 
     DialogResult result = MessageBox.Show 
      ("You must log in before you add an entry." 
      + Environment.NewLine + "You want to authenticate?", 
      "Information", MessageBoxButtons.YesNo, 
      MessageBoxIcon.Information); 

     if (result == DialogResult.Yes) 
     { 
      AuthenticationWindow authWindow = 
       new AuthenticationWindow(); 
      authWindow.ShowDialog(); 
      storedAuth = authWindow.Result; 

      AddEntryWindow addWindow = new AddEntryWindow 
       (this, storedAuth.UserName, storedAuth.Password); 
      addWindow.ShowDialog(); 
     } 
    } 
    else 
    { 
     AddEntryWindow addWindow = new AddEntryWindow 
      (this, storedAuth.UserName, storedAuth.Password); 
     addWindow.ShowDialog(); 
    } 
} 

回答

1

看看這個聲明 if(storedAuth == null)

如果你訪問的是prope空對象的rty。如果它是必需的對象,則將值賦給此對象,然後訪問UserName和Password。

這是錯誤的原因..你不應該在下面的語句中使用storedAuth.UserName,storedAuth.Password)。使用一些默認值,如「」或「默認」

AddEntryWindow addWindow = new AddEntryWindow (this, storedAuth.UserName, storedAuth.Password); addWindow.ShowDialog();

2

你所訪問的storedAuth性質,只是你上面檢查了storedAuth爲空,所以這段代碼是保證NullReferenceException ...

+0

Jeez,你說得對。我誤解了我自己的問題。我想打開註冊窗口。 – HelpNeeder

+0

...在addentry窗口之前。 – HelpNeeder

相關問題