So I've been working on a project and things were going fine until a NullReferenceException: Object reference not set to an instance of an object error began popping up only in the XAML designer for a View along with the stack trace. The project would run fine however.
To find the cause of this error I began commenting out code in both the offending View and ViewModel. Doing this would give inconsistent and odd results where I ended up with nearly all of the code commented out.
For instance, this would cause the error:
private async void FirmwareUpdateAsync()
{
bool isOK = true;
isOK = await Task.Run(() => _usb.DeviceHandshakeAsync());
if (!isOK)
{
// Left Empty
}
}
But this would not:
private async void FirmwareUpdateAsync()
{
bool isOK = true;
isOK = await Task.Run(() => _usb.DeviceHandshakeAsync());
/*if (!isOK)
{
// Left Empty
}*/
}
I spent two days trying to figure this out and trying to track down the exact cause when the error seemed to evolve into
Could not load file or assembly'[ProjectName], version=0.0.0.1, Culture=neutral, PublicKeyToken=null". The system cannot find the specified folder
and
Type reference cannot find type named'{clr-namespace:[ProjectName].ViewModels.SubViewModels;assembly = [ProjectName]}ErrorViewModel.
Again, this only occurs in the XAML designer and the line numbers for these errors are the declarations of the viewModels and subviews:
<Page.DataContext>
<vm:Pg7FirmwareUpdateViewModel/> <= Here
</Page.DataContext>
<Page.Resources>
<!-- Sets Progress Bar View -->
<DataTemplate DataType="{x:Type svm:ErrorViewModel}"> <= Here
<sv:ErrorView/>
</DataTemplate>
</Page.Resources>
Oddly, the number of these errors depends on how many views I open (always in the declarations), plus I can still run the project.
No amount of building or rebuilding or cleaning seems to matter, I've verified the project's dll is in the bin folder, I tried but failed to use fuslogvw (Can't figure out how to get a log for the project, I only managed to capture something with PowerShell.exe that I wasn't using), and I've looked at all of the 150+ questions on these errors I could find. Any clue what I should do to fix this?
UPDATE: After thinking about Mark's answer below, I excluded from project & moved the suspect View and ViewModels, and replaced them with brand new files of the same name. Everything seems OK until I add back the DataContext again. Is there a way the project can get corrupted so even though I'm cleaning and rebuilding the solution, VS is using bad data or something? This is maddening.
UPDATE2: Rather than blindly stab in the dark I figured I'd just copy all the files to a new folder and revert to my last commit which was working fine, then just copy the changes in and try again. After reverting I built old project and I have the same exact problem before attempting to copy in the new files!
That has to point to a corrupt file or something no?