help.axcms.netAxinom Logo
Save Save Chapter Send Feedback
KB10037: Exception: Object reference not set to an instance of an object
This is the most often exception encountered.


You encounter this exception when you run your application:

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


The cause is, that a method or a property of an object is called, which is set to null, for example:

Class object = null;

Often it happens because a null-reference is passed to a method, but the method relies on the parameter not being null and does not check the reference, but just call the method, as in this example:

public void UnsafeMethod(Class parameter)


Check if your variable is set to an object, rather than null and only than call the methods on it.

The real challenge is usually not to resolve the issue, but to found, which place in code causes this exception. Here you will need a StackTrace (it is provded with Exception details). It shows, in which method the error occured. If this method is in your code, it is relatively simple. You set a breakpoint, debug into the method and step until you see a null-reference.

If a chain of methods pass the same null-reference to each other and deep in the hierarchy an exeption occurs, you usually don't know which place is really the cause of the problem. A safe way to avoid this is to check the parameters at the very beginning of the method. E.g. to use assertions, if you know, the parameter does not allow nulls:

public void SaferMethod(Class notNullableParameter)

But what happens, if StackTrace goes deeper into code?

Normally you would not even see, in which method inside it crashes - only the assembly name.

Here you can help yourself with Debug DLLs. Axinom delivers (starting with version 9.1) for Solution Providers with Debug DLLs (under Components\Debug). It allows you to see which method exactly produced an exception. (To use debug DLLs make a backup of your bin-folder and copy the debug DLLs from Components\Debug to \bin). If you suspect a bug in code, report it to us.




APPLIES TO versions: all