Visual Studio 2015 error “Object reference not set to an instance of an object”

This fixed my razor intellisense problem and the error after installing the latest ASP.NET tools.

Here is how to fix it.

Delete the contents from the following folders:
C:\Users{user}\AppData\Local\Microsoft\VisualStudio
C:\Users{user}\AppData\Local\Microsoft\VSCommon

Navigate to the command line now and run devenv from either of these paths:
[x64] C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE
[x86] C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE

run devenv /resetuserdata from the command prompt.

Thanks to fka for the steps!

https://stackoverflow.com/questions/34084003/visual-studio-2015-error-object-reference-not-set-to-an-instance-of-an-object/34085345#34085345

Happy Coding!

The type initializer for +’System.Windows.PresentationSource’ threw an exception.

I had an issue with a 3rd party control. I couldn’t blame them too much since the issue only happened in the WPF control hosted in a MFC C++ application. brutal for sure.

Why host WPF in C++? That is a question for another time, but let’s just say its an app written in the early 90’s and still has been rewritten, just bolted on and refactored.

Well, I created the test application and it worked great on my machine at home. I loaded it into my work computer and blammo, it threw up on the get handle call. WTF?

If you step into the .NET source(slow but very helpful), you get this.

System.Runtime.Versioning.BinaryCompatibility.ParseTargetFrameworkMonikerIn/toEnum in the call stack. and  NULL parameter exception.

Turns out through some goofiness in VC++, it will insert a null value in a manifest.

The fix is really easy. Delete all the files in your AppData\Local\Temp folder under your profile.

Here is a link to a guy who wrote about it in much more detail, but man that was non-intuitive fix. It worked though, so I am happy!

Thanks Microsoft for stealing another hour of my life. Thanks Telerik for being there to steal a week trying to reproduce bugs in your silly slow controls.

Happy coding!

 

WPF Validation Errors not showing on initial load of control or window

I was setting validation errors on the initial load of my view model, and to my annoyance the window would show up like everything was great. The validation code was definitely running, the HasErrors was true, the dictionaries were filled with the errors but the form was like, “What errors?”  This irritated me. Well, WPF is stupid in this regard and doesn’t handle things properly. If you are like me, you set your datacontext after creating the view. Well, that is the logical way, but if you want validation you need to slightly alter that. I found a work around that fixes the problem, but is a little more annoying. Simply put, pass in your VM to the constructor of the view. Cache that, then set your DataContext(DC) on the load event. Sample code below:

public partial class AcceptRejectViewOldMeetsNew : UserControl
 {
   private object dcToSet = null;
   public AcceptRejectViewOldMeetsNew(object dc)
  {
    InitializeComponent();
    dcToSet = dc;
    this.Loaded += AcceptRejectViewOldMeetsNew_Loaded;
  }
  void AcceptRejectViewOldMeetsNew_Loaded(object sender, System.Windows.RoutedEventArgs e)
  {
    DataContext = dcToSet;
    }
 }

Happy Coding!

Fix: Kendo Grid Template gives error “Unable to get value of the property ‘replace’: object is null or undefined”

I got the lovely unhelpful error “Unable to get value of the property ‘replace’: object is null or undefined” from the Kendo Grid when I added a template to a column. This was a completely useless error that led me to staring at the javascript stack and walking through the minified code. Which, by the way, is very painful to look through obfuscated minified code.

After entirely too long analyzing the code, I finally figured out the root cause. I had an ever so small typo in my template retrieval declaration and it was returning back a null template. Why on earth didn’t the Telerik guys NULL check this and fire a friendly error? All of two lines of code would have saved me some trouble.

See, javascript is entirely too friendly to typing mistakes.

Here is the errant code:

    {
                             title: "Planning", width: "100px",                                       
                             template: kendo.template($("myPortfolio_PlanningTemplate").html())
                         },

Here is the fixed code.

    {
                             title: "Planning", width: "100px",                                       
                             template: kendo.template($("#myPortfolio_PlanningTemplate").html())
                         },

Painful lesson in remember to put in the hashtag for ID lookups. Easy to forget when in a hurry.

Happy Coding!

How To Fix:Error 39 The type or namespace name ‘Contracts’ does not exist in the namespace ‘XXX’ (are you missing an assembly reference?)

So….Just got done mentally disintegrating my computer and various items around it. This lovely error kept appearing to tell me ABSOLUTELY NOTHING.

I checked my references, of course they were included. It’s how I had intellisense, and every other feature that leads me to believe everything is A OK. That is, until you actually compile. You then get the awesomely descriptive error above.

Oh, It appears descriptive right? You say, oh, I must have done something wrong and somehow removed the assembly reference. Let me check it? Nope, it’s there.

Well, it turns out the answer is simple and the stupid compiler could have added one more suggestion. Here is how I would write it.

The type or namespace name ‘Contracts’ does not exist in the namespace ‘XXX’ (are you missing an assembly reference? Are the assemblies built using the same .NET Runtime Profile?)

Yup! One assembly was using the .NET 4.0 CLIENT profile and the other the .NET 4 Full profile. Awesome huh!!!

So, if you come across this with one eye bleeding because you stabbed yourself and missing hair, now you know the simple resolution.

image

 

Happy Coding!

Config file issue to make you rip hair out

I spent about 2 hours staring at this lovely cryptic error:
Extension element ‘silverlightFaults’ cannot be added to this element.  Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions

I had it setup correctly, but why would the system not compile it?

This was the registration:

<add name="silverlightFaults"
type="Seekford.SilverlightFaultBehavior.SilverlightFaultBehavior,Seekford.SilverlightFaultBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7073c134a4dabf61" />

So? What is wrong with this picture? Nothing unless you are an unforgiving parse engine that exists to make life difficult.

I had to add a space. Yes, a space between the class and assembly name and BAM!, it worked.

<add name="silverlightFaults"
type="Seekford.SilverlightFaultBehavior.SilverlightFaultBehavior, Seekford.SilverlightFaultBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7073c134a4dabf61" />

What a pile of #$%!!

Anyway, remember your spacing.

Happy Coding!

DateTimePicker Validation Error Block Calendar Button

I had a lot of user complaints about the tooltip for the DateTime Picker for Silverlight showing the error tooltip over the Calendar button. It made it so that it was next to impossible to actually select a date, since you couldn’t click the button.

I opened the DateTimePicker template and voila! The tard who made such a nice control set the error template to sit next to the TextBox instead of the Button.

Simple fix. Just generate the  DateTimePicker template (Expression Blend) and change the following section:
i.e. Search for ToolTipService.ToolTip

<ToolTipService.ToolTip>
<ToolTip x:Name="validationTooltip" Template="{StaticResource CommonValidationToolTipTemplate}" Placement="Right" PlacementTarget="{Binding ElementName=Button}" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<ToolTip.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>true</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToolTip.Triggers>
</ToolTip>
</ToolTipService.ToolTip>

Using Change the above to Button instead of TextBox. Super easy!

Happy Coding!

DelegateCommand in Prism loses eventhandler and CanExecute never gets called

I beat my head against the all for half a day wondering why the DelegateCommand doesn’t work in my Silverlight application. Very frustrating. I traced the code in and somehow the event handler loses its reference. I am guessing it something to do with the WeakReference being used to store the event reference.

Anyway, after wasting entirely too much time with it, I just said screw it and went to using the RelayCommand class I  found. It works great and all the problems I had went away. When I have free time, I will go back and see what the true cause for the reference drops were.

Here is the RelayCommand code, if you need it. I got it from an MSDN article a while back for MVVM.
Interface

using System.Windows.Input;
namespace Seekford.Infrastructure.Commanding
{
 /// <summary>
 /// Support for commanding.
 /// </summary>
 public interface IRelayCommand : ICommand
 {
 /// <summary>
 /// Raises the can execute changed.
 /// </summary>
 void RaiseCanExecuteChanged();
 /// <summary>
 /// Determines whether this instance can execute the specified parameter.
 /// </summary>
 /// <param name="parameter">The parameter.</param>
 /// <returns>
 ///     <c>true</c> if this instance can execute the specified parameter; otherwise, <c>false</c>.
 /// </returns>
 bool CanExecute(object parameter);
 /// <summary>
 /// Executes the specified parameter.
 /// </summary>
 /// <param name="parameter">The parameter.</param>
 void Execute(object parameter);
 }
}

Class Definition

using System;
using System.Windows.Input;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;

namespace Seekford.Infrastructure.Commanding
{

 /// <summary>
 /// A command whose sole purpose is to relay its functionality to other
 /// objects by invoking delegates. The default return value for the CanExecute
 /// method is 'true'.  This class does not allow you to accept command parameters in the
 /// Execute and CanExecute callback methods.
 /// </summary>
 public class RelayCommand :  IRelayCommand
 {
 private readonly Action _execute;
 private readonly Func<bool> _canExecute;

 /// <summary>
 /// Initializes a new instance of the RelayCommand class that
 /// can always execute.
 /// </summary>
 /// <param name="execute">The execution logic.</param>
 /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
 public RelayCommand(Action execute)
 : this(execute, null)
 {
 }

 /// <summary>
 /// Initializes a new instance of the RelayCommand class.
 /// </summary>
 /// <param name="execute">The execution logic.</param>
 /// <param name="canExecute">The execution status logic.</param>
 /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
 public RelayCommand(Action execute, Func<bool> canExecute)
 {
 if (execute == null)
 {
 throw new ArgumentNullException("execute");
 }

 _execute = execute;
 _canExecute = canExecute;
 }

#if SILVERLIGHT
 /// <summary>
 /// Occurs when changes occur that affect whether the command should execute.
 /// </summary>
 public event EventHandler CanExecuteChanged;
#else
 /// <summary>
 /// Occurs when changes occur that affect whether the command should execute.
 /// </summary>
 public event EventHandler CanExecuteChanged
 {
 add
 {
 if (_canExecute != null)
 {
 CommandManager.RequerySuggested += value;
 }
 }

 remove
 {
 if (_canExecute != null)
 {
 CommandManager.RequerySuggested -= value;
 }
 }
 }
#endif

 /// <summary>
 /// Raises the <see cref="CanExecuteChanged" /> event.
 /// </summary>
 [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
 Justification = "The this keyword is used in the Silverlight version")]
 [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
 Justification = "This cannot be an event")]
 public void RaiseCanExecuteChanged()
 {
#if SILVERLIGHT
 var handler = CanExecuteChanged;
 if (handler != null)
 {
 handler(this, EventArgs.Empty);
 }
#else
 CommandManager.InvalidateRequerySuggested();
#endif
 }

 /// <summary>
 /// Defines the method that determines whether the command can execute in its current state.
 /// </summary>
 /// <param name="parameter">This parameter will always be ignored.</param>
 /// <returns>true if this command can be executed; otherwise, false.</returns>
 [DebuggerStepThrough]
 public bool CanExecute(object parameter)
 {
 return _canExecute == null ? true : _canExecute();
 }

 /// <summary>
 /// Defines the method to be called when the command is invoked.
 /// </summary>
 /// <param name="parameter">This parameter will always be ignored.</param>
 public void Execute(object parameter)
 {
 _execute();
 }
 }
}

Lotus Notes JVM Terminated Error=8096

We all know Lotus Notes is a big pile of crap. Well, I kept getting this lovely error JVM Terminated Error=8096 after a power failure.

Good times.

It turns out, that after much head banging, simply blowing out your TEMP folder will fix it.

Just open your TEMP folder and delete the folder called XPDPLAT.

You will then be on your way.

Easy way to get there: Type %TEMP% into your Explorer and hit ENTER.
Happy Coding!

Dotfuscator and KernelBase crashes, .NET crash with your application or service after obfuscation

Quick Tip: Don’t let Dotfuscator auto-sign your assemblies without specifying the key file explicitly.

I spent the last hour beating my head into the wall, again, trying to figure out why Dotfuscator was creating a crap exe. I turned off every option, no help.I figured it must be because I am writing a windows service, so I tried it on a normal exe of mine.

Everything worked fine there. The pain , oh the pain. Well, it finally dawns on me to try turning off my digital signature on my service. Voila! Dotfuscator no longer create the crap exe that threw awesome KernelBase.dll exceptions.

So, it turns out the Dotfuscator’s auto re-sign sucks. The trick is that if you do the re-sign, make sure you explicitly specify the signing key path.