Object reference not set to an instance of an object.

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am trying to write a trivia game to run in a skype chat!

it will be a bot to ask random questions and tell the users in the chat if they answered correctly and keep score.....
I am getting a error at this line of code

if (pMessage.Body.ToString().ToLower() != AnswerT.ToString().ToLower())


I'm not sure why.....

here is the entire function...

this is in the constructor......

skype = new Skype();
            skype.Attach(8, false);
            skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);


 public void skype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)        {
            user = pMessage.Sender.FullName;
            // string Asked = AskQuestion();
            string Chat_Name = pMessage.ChatName;
            string what_said = pMessage.Body.Substring(0);


            if (!lstUsers.Items.Contains(user))
            {
                lstUsers.Items.Add(user);
                users.Add(user);


            }
            if (pMessage.ChatName == Chat_Name)
            {
                if (Status == TChatMessageStatus.cmsReceived | Status == TChatMessageStatus.cmsSent)
                {
                    if (pMessage.Body.ToString().ToLower() != AnswerT.ToString().ToLower())
                    {
                        ProcessCommand(what_said);
                    }
                        if (pMessage.Body.ToString().ToLower() == AnswerT.ToString().ToLower())
                        {
                            if (!userScore.ContainsKey(pMessage.Sender.FullName))
                            {
                                userScore.Add(pMessage.Sender.FullName, 100);


                                pMessage.Chat.SendMessage("[" + user + "]Answered Correct!");
                                pMessage.Chat.SendMessage("[" + user + "] answered [" + ProcessCorrect() + "] correct questions");
                                pMessage.Chat.SendMessage("[" + user + "] your score: " + userScore[user]);
                                pMessage.Chat.SendMessage(AskQuestion());
                                MessageBox.Show(AnswerT);


                                return;
                            }
                            else
                            {


                                userScore[user] = userScore[user] + 100;
                                pMessage.Chat.SendMessage("[" + user + "]Answered Correct!");
                                pMessage.Chat.SendMessage("[" + user + "] answered [" + ProcessCorrect() + "] correct questions");
                                pMessage.Chat.SendMessage("[" + user + "] your score: " + userScore[user]);
                                pMessage.Chat.SendMessage(AskQuestion());
                                MessageBox.Show(AnswerT);
                            }
                         


                        }


                     }


                   }   
                }


thank you for any help!
-InkedGFX
 
Last edited:
Firstly, it's bad practice to use ToLower or ToUpper in order to compare strings. In some languages it is required but in .NET languages it's not so it should not be done. If you want to perform a case-insensitive comparison then that's exactly what you should do, e.g.
if (string.Equals(pMessage.Body.ToString(), AnswerT.ToString(), StringComparison.CurrentCultureIgnoreCase))
I'm guessing that those two ToString calls are redundant too, because pMessage.Body and AnswerT are probably already strings.

Also, your use of 'if' statements is poor. You have:
if (pMessage.Body.ToString().ToLower() != AnswerT.ToString().ToLower())
{
    // ...
}
if (pMessage.Body.ToString().ToLower() == AnswerT.ToString().ToLower())
{
    // ...
}
You're basically performing the same test twice and you're performing the second one even if it's not needed. If you want to do one thing if a condition is true and another if it's false then you test the condition once only and use an if...else statement. In your case, you could test for equality or inequality but, in the case of if...else statements, it is good practice to ALWAYS test for equality and only test for inequality if you want to do something only if they values are not equal:
if (pMessage.Body.ToString().ToLower() == AnswerT.ToString().ToLower())
{
    // ...
}
else
{
    // ...
}
Finally, regarding the question, NullReferenceExceptions are actually one of the easiest exceptions to diagnose so you should be able to do it for yourself. First of all, you need to test each reference on the offending line to see which are null. For any that are null, then check whether you are trying to access a member of that object that doesn't exist, because that's the problem. In your code, the possible null references are pMessage, pMessage.Body and AnswerT. Once you determine which is null, you simply backtrack through the code to where you expected a value to be set. It's most likely on that line that your issue is to be found but it will definitely be somewhere between that line and where the exception was thrown. If the issue is not apparent then you set a break point and step through the code, watching that reference to see when and where it's null.
 
yes , I see your point about comparing twice..I will start using the else statement......

as far as the error NullReferenceException ...I figured it out...I wasn't actually asking a question so when the program tried to compare to see if what said in the chat equals the answer...the answer variable was null......so I added this

if (AnswerT != null)
{
doSomething
}
else
doSomething

thank you for your help once again

-InkedGFX
 
Back
Top Bottom