hi all i'm having a hard time trying to understand why some code that i have crash. The problematic instructions are like this:
First try:
bool aers = vm_SessionTab.IncrementalConstruction; // != null
bool chua = IncrementalCB.Checked; // != null
if (aers) IncrementalCB.CheckState = CheckState.Checked; // crash, null reference exception
Second try:
bool aers = vm_SessionTab.IncrementalConstruction; // != null
bool chua = IncrementalCB.Checked; // != null
IncrementalCB.Checked = vm_SessionTab.IncrementalConstruction //crash, null reference exception
IncrementalCB is a CheckBox. vm_SessionTab is a personalized object that contains the status that i want to show. When i step in with the debugger, i found that "aers" y "chua" are distinct of null. Then in the next two attempts the program crash telling me that it occurs a null reference exception. I'm asking how that is happend. I check explicitly that both values are distinct of null, :S. The complete code is rather big, but these are the relevant lines.
More info:
i'm really sure that IncrementalCB is != null but if i create another checkbox previous to the code that i showed then no exception is throw and the control is showed correctly. Why i need to recreate the control if it is already there? This works:
IncrementalCB = new CheckBox(); //i think this is unnecesary, it exists.
bool aers = vm_SessionTab.IncrementalConstruction;
bool chua = IncrementalCB.Checked;
if (aers) IncrementalCB.CheckState = CheckState.Checked;
edited to add…
Thanks guys to suggest me see the stacktrace. In that i see the terrible error that i was making. In a pseudo code explination this was my error: (I try to apply the MVVP pattern)
Call the View Constructor
Create the Presenter that manage the view.
at the end of the Presenter constructor i callback a method to show in the View the basic UI elements.
callback: .
in the view i load differents UI elements ... presenter contructor not yet finalized
i modify the value of the checkbox, trying to do it ...
directly.
ERROR->the modification is not realized directly and the ...
event handler of the Checked_Changed event is raised.
The presenter has te responsability to perform ...
the change, but.................................> ... presenter constructor not yet finalized
So the result is a Null reference exception when the not yet completed presenter is ask to complete the change. As i don't have access to an inner Checked field to do a direct change without the event Checked_Changed being raised i need to move out the callback. The error was a little difficult to see it because when i jump in with f11 in the debugger, the call of the event handler for the event is not showed, so i only see till the line where every object was in a good way to perform their actions