I have a very strange case where I have a null parameter in my class that I attempt to access (and hence cause a NullReferenceException which is expected) however the debugger reports a field from another entirely unrelated class is null instead. I have tried to reproduce this however I cannot. Can anyone explain what is going on?
I have made a new small test project with the same classes.
namespace WpfApp6
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Test_Click(object sender, RoutedEventArgs e)
{
var container = new SegmentedBinaryData();
var temp = container.StringData[0x123,10];
}
}
public class SegmentedBinaryData
{
public StringData StringData;
}
public class StringData
{
public string this[int address, int maxLength]
{
get => "lol";
set { }
}
}
}
If you run this test project and press the Test button you get the following exception:
System.NullReferenceException: 'Object reference not set to an
instance of an object.' container.StringData was null.
This makes perfect sense. In my actual project I get the following exception:
System.NullReferenceException: 'Object reference not set to an
instance of an object.' container.<SmoothXEnabled>k__BackingField was null.
SmoothXEnabled is a boolean property from another class that is not even in the same namespace not to mention it is not even referenced by this assembly in any way.
It should say "StringData" was null but it does not. Why is the VS2019 debugger getting confused here? I have tried debugging with VS2017 and I get the same error
I debugged the program in dnspy and it just says the generic "Object reference not set to an instance of an object." error instead which makes more sense and is what I expect the visual studio debugger to report.
SmoothXEnabled is defined in this class (from an entirely different assembly and DLL)
public abstract class ParameterViewModel : BaseViewModel, IDisposable
{
public virtual bool SmoothTableEnabled { get; set; }
public virtual bool SmoothEnabled { get; set; }
public virtual bool SmoothXEnabled { get; set; }
If I continue to delete classes from the project eg I delete the class that contains SmoothXEnabled the debugger reports the following error instead:
System.NullReferenceException: 'Object reference not set to an
instance of an object.'container.CheckSumAddr was null.
CheckSumAddr is at least from the same namespace however it is also once again, not even instantiated or referenced from within the class that the NullReference is thrown. I can keep going through my program deleting classes and variables and the debugger reports a different random variable from a random class is null each time.
If I add a field to SegmentedBinaryData I can shift the "random" field that is named in the exception. It is almost like the debugger is accessing the "null" pointer, adding the offset (because of the indexer maybe?) then reaching another random variable?
Can anyone explain why the visual studio debugger is performing in this manner but the dnspy debugger is not?
edit: The reason this is important to resolve is the wrong field name being reported wasted a lot of time trying to track down how that could possibly be null. Had no field name been given I would have seen StringData was null and fixed the issue immediately. It was only after I debugged with dnspy did I realise that StringData was actually the problem.