2012-06-25 44 views
0

首先,我在這裏是新的,並與csharp新。在爲了學習csharp做一些練習時只是精疲力竭。無法找到解決方案,爲什麼我得到的錯誤「NullReferenceException是未處理」和按鈕數組NullReferenceException未處理

1)如何克服這一點?根據我的研究,它與初始化有關,但無法做到。我調試並觀察值的所有按鈕得到空值是因爲這個?我該如何解決它?該怎麼辦?哪些代碼要添加?我想學習所有的要點,想成爲專家:P和人們可以優化代碼,歡迎所有額外的信息。)

2)爲什麼編譯器不顯示錯誤,但運行代碼時出現錯誤?

確定現在我的代碼吹塑

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace temp1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Button[] btn = new Button[5]; 

      for (int i = 0; i < 5; i++) 
      { 
       btn[i] = new Button(); 
       btn[i].Width = 50; 
       btn[i].Height = 50; 
       flowLayoutPanel1.Controls.Add(btn[i]); 
       btn[i].Click += new EventHandler(btn_Click); 
      } 
     } 

     void btn_Click(object sender, EventArgs e) 
     { 
      Button[] btn = sender as Button[]; 
      btn[3].Text = "button name changed"; // here problems occurs 
      //btn[3].BackColor = Color.Red; // here problems occurs 
      // btn[3].PerformClick(); // here problems occurs 
     } 
    } 
} 

回答

3

它應該是:

void btn_Click(object sender, EventArgs e) 
{ 
    Button btn = sender as Button; 
    btn.Text = "button name changed"; // here problems occurs 
} 

發送者是該按鈕本身,而不是在陣列

UPDATE根據COMMENT:

你應該這樣做:

public partial class Form1 : Form 
{ 
    private Button[] m_ButtonsArray; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     m_ButtonsArray = new Button[5]; 

     for (int i = 0; i < 5; i++) 
     { 
      m_ButtonsArray [i] = new Button(); 
      m_ButtonsArray [i].Width = 50; 
      m_ButtonsArray [i].Height = 50; 
      flowLayoutPanel1.Controls.Add(m_ButtonsArray [i]); 
      m_ButtonsArray [i].Click += new EventHandler(btn_Click); 
     } 
    } 

    void btn_Click(object sender, EventArgs e) 
    { 
     m_ButtonsArray[3].Text = "button name changed"; // here problems occurs 
    } 
} 

UPDATE - 更改被點擊5號鍵時,第二個按鈕:

public partial class Form1 : Form 
{ 
    private Button[] m_ButtonsArray; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     m_ButtonsArray = new Button[5]; 

     for (int i = 0; i < 5; i++) 
     { 
      m_ButtonsArray [i] = new Button(); 
      m_ButtonsArray [i].Width = 50; 
      m_ButtonsArray [i].Height = 50 
      m_ButtonsArray [i].Tag = i;; 
      flowLayoutPanel1.Controls.Add(m_ButtonsArray [i]); 
      m_ButtonsArray [i].Click += new EventHandler(btn_Click); 
     } 
    } 

    void btn_Click(object sender, EventArgs e) 
    { 
     Button btn = (Button)sender; 
     if (((int)btn.Tag) == 5) 
     { 
      m_ButtonsArray[2].Text = "your text here"; 
     } 
    } 
} 

解釋: 當我創建按鈕,我添加索引作爲標記(按鈕知道它的指數與Tag property) 單擊按鈕時,我檢查發件人對象 - 將其保存在Tag中的索引保存爲Button。如果Tag值爲5的話,我引用的第二個按鈕(ⅰ可以引用它雖然m_ButtonsArray)和改變它

+0

感謝,但那我該怎麼到達第3個按鈕並使其顏色發生變化 –

+0

查看更新 – eyossi

+0

yuppi yes它的工作表示感謝。所以在我的代碼中,如果取消Button [] btn = sender作爲Button [];部分按鈕點擊並移動Button [] btn = new Button [5];到一個上面的form1load部分它也是wokrs。謝謝,但是當我使用Button btn = sender作爲Button時; ???我的意思是在哪種情況下,我使用該聲明?我沒有充分理解就寫了這個陳述。現在我看到我不應該寫它。但在哪些情況下我使用它。另一個問題是。是否可以改變按鈕文本的名稱,當我點擊特定的按鈕,讓坐在第2個按鈕? –

0

此代碼是哪裏的問題開始的值:

Button[] btn = sender as Button[]; 

在有編譯時沒有向編譯器指示object類型的sender不會投射到Button的陣列。但是在運行時它不會像這樣轉換,因爲它不是一個數組。這只是一個Button

Button btn = sender as Button; 

的處理方法(btn_Click)不是單獨處理在同一時間全部按鈕,而是每個單獨的一個。當單擊按鈕時,該按鈕本身會調用處理程序方法。

+0

何時我讓這個編譯器在btn [3] .Text =「button name changed」部分給出錯誤,我想到達第3個按鈕並改變顏色 –

+0

@modestandcutegirl:因爲沒有引用'Button' (它是動態添加的),你需要在'flowLayoutPanel1.Controls'中找到它,它存在於其他按鈕(以及可能的其他控件)中。這就是它只能在處理程序方法中知道。 – David

+0

謝謝。我想感謝所有,但不能回覆我的信息味精,因爲即時新的和沒有太多的聲譽。所以即時感謝你。感謝你的回覆。 –

0

(其他已經正確地回答了爲什麼這個異常被拋出)

的錯誤不能被編譯器顯示,因爲你正迫使來自物體的轉換(使用as)到別的東西。編譯器不知道對象是什麼,所以不知道會發生錯誤。

如果您在代碼中手動調用該方法,並將其作爲第一個參數傳遞給一組按鈕,則代碼不會崩潰。

在這種情況下,一件好事就是使用標準轉換而不是as拋出的異常會更加明確,並且會在正確的行上發生,而不是讓空值傳播並使後者失敗。

對你的代碼的一個小小的評論是你的數組沒有用,你不會在任何地方使用它,只要加載函數完成執行就會從內存中刪除它(很不完全,但你可以閱讀有關垃圾收藏家如果你想了解更多細節)。

+0

重點是,我想要達到讓我說數組中的第3個按鈕,並使其顏色變化時,我點擊按鈕?這只是和我編造的練習。因爲我可以有100個按鈕,我做了循環,並把它放到一個數組,當我點擊一個按鈕,我可能想讓第2索引(第3)按鈕來更改名稱或更改顏色。另一個問題是,如果我想爲第三個按鈕着色,當我點擊一個特定的可以說第五個按鈕?並感謝所有回答你的人。希望我的問題現在或多或少清楚。 –

+0

感謝你的虛擬黑狐來獲取信息。你也可以閱讀我對「眼鏡」的回覆。如果你有一些說我們歡迎。謝謝你們所有的回覆。 –

1

可以解決這樣的太...它是eyossi的解決方案

公共部分Form1類非常相似:表格 { 私人

public Form1() 
{ 
    InitializeComponent(); 
} 
Button[] m_Buttons = Array new Button[5]; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    for (int i = 0; i < 5; i++) 
    { 
     m_ButtonsArray [i] = new Button(); 
     m_ButtonsArray [i].Width = 50; 
     m_ButtonsArray [i].Height = 50; 
     flowLayoutPanel1.Controls.Add(m_ButtonsArray [i]); 
     m_ButtonsArray [i].Click += new EventHandler(btn_Click); 
    } 
} 

void btn_Click(object sender, EventArgs e) 
{ 
    m_ButtonsArray[3].Text = "button name changed"; // here problems occurs 
} 

}