-1
Line 24:             if (word.Length<3)

Line 25:             {
Line 26:                 Label1.Visible = true;

Source File: C:\Users\c-tac\Documents\Visual Studio 2010\Projects\telephone\telephone\show.aspx.cs    Line: 24

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • can you show all the code initializing the Word object? – R.C Jul 21 '13 at 04:49
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 21 '13 at 05:17

2 Answers2

0

Seems pretty obvious, word is not set to an instance; in other words word is null.

Put a check to make sure word is not used unless it is instantiated as something, like this:

if(word != null)
{
    // Do stuff with word, because you know it actually exists now
}

Note: This is referred to as defensive programming and will eliminate almost all NullReferenceExceptions in your code. It also has the added benefit of making you think about what you should do with your code in case a particular object is null (such as should this should be reported to the user, should this cause the application to end, etc.).

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

NullReferenceException happens when you want to do something like iteration with an element that normally you can't do it with null value. You should check your code and if the value is null make sure that you assign a default value to in before you other stuff. or just do the easy way and use try and catch.

 try
 {
      // do your stuff with word
 }
 catch
 {
      // handle the null exception
 }

although based on your code null means that your word length is zero. so you can do this as well.

 if(word != null || word.lenght<3)
 {
      // do your thing
 }
Daniel
  • 3,322
  • 5
  • 30
  • 40
  • 1
    I am saddened that you would promote the "easy way" of wrapping code in try...catch blocks and then catching `NullReferenceException`. This is a poor design idea for several reasons, most of all that it ignores fixing the underlying problem and passes the hot potato to someone else and, second, it promotes having code throw `NullReferenceException`s to be caught by said code. – Karl Anderson Jul 21 '13 at 04:25