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.

SYMPTOMS

You encounter this exception when you run your application:

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

CAUSES

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

Class object = null;
object.DoSomething();

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)
{
    ...
parameter.DoSomething();
}

SOLUTION

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)
{
    Debug.Assert(notNullableParameter!=null);
    notNullableParameter.DoSomething();
    ....
}

But what happens, if StackTrace goes deeper into AxCMS.net code?

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

Here you can help yourself with Debug DLLs. Axinom delivers AxCMS.net (starting with version 9.1) for AxCMS.net 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 AxCMS.net code, report it to us.

MORE INFORMATION

 

 

APPLIES TO

AxCMS.net versions: all