Apparently the vast majority of errors in code are null reference exceptions. Are there any general techniques to avoid encountering null reference errors?
Unless I
Tools that can help
There are also several libraries that can help. Microsoft Code Contracts was mentioned above.
Some other tools include Resharper which can provide you with warnings while you are writing code, especially if you use their attribute: NotNullAttribute
There's also PostSharp which will allow you to just use attributes like this:
public void DoSometing([NotNull] obj)
By doing that and making PostSharp part of your build process obj will be checked for null at runtime. See: PostSharp null check
The Fody code-weaving project has a plug-in for implementing null guards.