How do I fix NullReferenceException


Exceptions have been created as a tool to signal exceptional non-fatal conditions up the call order. Reference variables in c# and JavaScript are similar in concept to pointers in C and C++. Reference types default to null to indicate that they are not referencing any object. Hence, if you try and access the object that is being referenced and there isn't one, you will get a NullReferenceException.

The NullReferenceException is designed as a valid runtime condition that can be thrown and caught in normal program flow. It indicates that you are trying to access member fields, or function types, on an object reference that points to null. That means the reference to an Object which is not initialized.

For Ex:

SqlConnection connection = null;
C# Interview questions

When you run this code, you will get :

System.NullReferenceException: Object reference not set to an instance of an object.

You can avoid this error by coding like this:

if (connection != null)
.Net Interview questions and answers

Note: In order to avoid this error you should always initialize your objects before you try to do anything with them.

Object reference not set to an instance of an object

The message "Object not set to instance of Object" means that you are trying to use an object which has not been initialized. That is, you either set it to null, or you never set it to anything at all. The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference. The reference is not initialized (or it was initialized, but is no longer initialized). This points to one of the following:

Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)

Something which your code assumed would initialize an object, did not

Possibly, other code prematurely invalidated an object still in use

For Ex:

string str = null;

This will throw a NullReferenceException at the second line, because you can't call the instance method ToUpper() on a string reference pointing to null.

It's usually better to avoid a NullReferenceException than to handle it after it occurs. Handling an exception can make your code harder to maintain and understand, and can sometimes introduce new bugs.

Exception Handling

Exceptions are the occurrence of some condition that changes the normal flow of execution. In .NET languages , Structured Exceptions handling is a fundamental part of Common Language Runtime. More about.... Exception Handling

How to throw Exception

Exception objects that describe an error are created and then thrown with the throw keyword. By using a throw statement inside a catch block, we can change the resulting exception. More about.... Throw Exception System level Vs Application level Exceptions

Exceptions that are thrown by .NET framework are called as system exceptions. These errors are non-recoverable or fatal errors like ArgumentOutOfRangeException, IndexOutOfRangeException, StackOverflowException etc. Application Exception serves as the base class for all application specific exception classes. It derives from Exception but does not provide any extended functionality. You should derive your custom application exceptions from Application Exception. Application exception are used when we want to define user defined exception. More about.... System level Vs Application level Exceptions

How to create a custom exception

If you want users to be able to programmatically distinguish between some error conditions, you should create your own custom exceptions. It will simplify and improve the error handling and thus increase the overall code quality. More about.... Create a custom exception

Difference between Exception and Error

An exception is an Object of a type deriving from the System.Exception class. SystemException is thrown by the CLR (Common Language Runtime) when errors occur that are nonfatal and recoverable by user programs. More about.... Exception and Error

NEXT.....Custom Exception