Hi,
I'm getting the following error: NullReferenceExceptionWasUnhandled on if (messageUserList[i].Contains("_0")).
I can't seem to find where the data is null. I've tried debugging using breakpoint yet no luck.

Basically what I intend to do is get the text from a particular textbox txtSendMsg and write it to messageList at position messageListPos. Then the textbox will be cleared. The current position messageListPos and current user currUser (which in this case is 0) will be writen to another array messageUserList at position messageListPos. So far the program identifies taht the value at messageUserList[messageListPos] is '0_0', (this I figured out from debugging). Later on the program will loop through the messageUserList until the length of messageList has been reached. If messageUserList at position i contains '_0' the program should apply the following effects and so on.

lbxChatHist is actually a TextBlock not a ListBox.

PS: I'm using WPF

string[] messageList = new string[System.Int16.MaxValue];
string[] messageUserList = new string[System.Int16.MaxValue];

byte[] userId = new byte[15];

byte currUser = 0;

int messageListPos = 0;

private void btnSendMsg_Click(object sender, RoutedEventArgs e)
        {
            messageList[messageListPos] = txtSendMsg.Text;

            txtSendMsg.Clear();

            messageUserList[messageListPos] = messageListPos.ToString() + "_" + currUser.ToString();

            for (int i = 0; i < messageList.Length; i ++)
            {
                if (messageUserList[i].Contains("_0"))
                {
                    lbxChatHist.FontFamily = new FontFamily("Century Gothic");
                    lbxChatHist.Foreground = Brushes.DeepSkyBlue;
                    lbxChatHist.FontWeight = FontWeights.DemiBold;
                    lbxChatHist.FontStyle = FontStyles.Normal;

                    lbxChatHist.Text += messageList[i];
                }

                lbxChatHist.Text += Environment.NewLine;
            }

            messageListPos++;
        }

Thanks,
Isaac Hili

Recommended Answers

All 2 Replies

I think your problem is here:

for (int i = 0; i < messageList.Length; i ++)
{
    if (messageUserList[i].Contains("_0"))
    {
        .
        .
        .
    }
}

You probably need to set the guard barrier i < messageList.Length to i <= messageListPos. You have initialized the strings in messageList and messageUserList up to the value of messageUserPos, but everything beyond that is not initialized. The messageList.Length I believe would contain the number of slots in the list, which is the size of 16-bit integer - about 32000 items.

That said, before you start your loops you could first set every slot in messageList and messageUserList to an empty string. It will make your code slower because each iteration through the outer loop will require a full iteration in the inner loop, looking for the "_0" string. So, my advice is to rethink your nesting of these operations to determine if there is a more efficient way to accomplish your purposes.

Ok thanks a lot. And coming to think of it yes I really should find another way of tackling this problem.

Cheers :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.