user1146887
user1146887

Reputation:

Change Button Text - Windows Phone 7

How do you change the text of a button in Windows Phone 7 and C#? NullPointer Exception too, if I am doing the changing of the button text right, what is the problem?

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]);
    }
}

Upvotes: 0

Views: 3376

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

As Justin said earlier, you have just created an Array of the Type of Button, You have not added any Button's to the Array as of yet. You need to explicitly set each index of the Array to a Button: Try doing something like this.

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

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245399

You're getting the NullReferenceException because, while you have created the new Button array, you haven't initialized any of the elements of that array (each element is still null).

Upvotes: 4

Related Questions