What is the meaning of a NullReferenceException

Possible duplicate:
What is a NullReferenceException in .NET?

For example, " System.NullReferenceExceptionwas unprocessed," with the message "Object reference not installed in the object instance."

What is the meaning of this exception and how can it be solved?

+3
source share
6 answers

This means that you tried to access a member of something that is not there:

string s = null;
int i = s.Length; // boom

Just fix the thing that is null. Either make it non-empty, or do a zero test first.

There is also boundary code related to Nullable<T>, generics and a general restriction new- a little unlikely (but damn it, I hit this question!).

+14

.NET... , , (null). , .

+8

, -, null, :

class Test
{

   public object SomeProp
   {
      get;
      set;
   }

}

new Test().SomeProp.ToString()

SomeProp null a NullReferenceException. , , , - , .

+2

, , :

string temp;
int len = temp.Length; // throws NullReferenceException; temp is null

string temp2 = "some string";
int len2 = temp2.Length; // this works well; temp is a string
+1

.

string s = null;
s = s.ToUpper();
+1

- , :)

- , .

what you should do:

MyClass c = new MyClass();

what did you do:

MyClass c;
c.Blah();
+1
source

Source: https://habr.com/ru/post/1711059/


All Articles