2012-01-13 100 views
0

如何更改Windows Phone 7和C#中按鈕的文本? NullPointer異常也是,如果我正在更改按鈕文本的權利,那麼問題是什麼?更改按鈕文本 - Windows Phone 7

public void CreateWords(Word[] theWords) 
{ 
    listOfWords = new Button[theWords.Length]; //Button Array 
    textBlock1.Text = theWords[0].getWord(); //Totally Works 
    for (int i = 0; i < theWords.Length; i++) 
    { 
     listOfWords[i].Content = theWords[0].getWord(); //NullPointer Exception 
    } 

    for (int i = 0; i < theWords.Length; i++) 
    { 
     stackWords.Children.Add(listOfWords[i]); 
    } 
} 

回答

4

你得到NullReferenceException因爲,當你創建新的按鈕配置,你還沒有初始化任何數組的元素(每個元素仍然是空)的。

+0

謝謝大家,:P – 2012-01-13 03:54:11

1

正如賈斯汀在前面所說,你剛剛創建了一個按鈕類型的數組,你還沒有添加任何按鈕到數組中。你需要明確地設置數組的每個索引到一個按鈕:嘗試做這樣的事情。

for (int i = 0; i < theWords.Length; i++) 
{ 
    listOfWords[i] = new Button(); 
    listOfWords[i].Content = theWords[0].getWord(); //NullPointer Exception 
}