0

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

Dori
  • 915
  • 1
  • 12
  • 20
mjsr
  • 7,410
  • 18
  • 57
  • 83
  • 3
    Post the stack trace of the exception. – Hans Passant Apr 16 '11 at 21:52
  • *It's always the same cause.* IncrementalCB is null or CheckState is null or vm_SessionTab is null. Here's the algorithm: NullReferenceException? Check left of dots. – R. Martinho Fernandes Apr 16 '11 at 21:56
  • How did you determine that IncrementalCB is not null? The fact that the second example works leaves little choice. – R. Martinho Fernandes Apr 16 '11 at 21:58
  • @MartinhoFernandes i step into those instructions with the debugger. I know that they are not null. I also in others attempts using a Mbox print those values. – mjsr Apr 16 '11 at 22:06
  • 1
    You'll have to show more code. Or at least a *stack trace*. With the code and values you show, there's nothing we can do to help. Are you messing around with threads by any chance? – R. Martinho Fernandes Apr 16 '11 at 22:08
  • @voodoomsr: Does it work if you remove all breakpoints? Ctrl+Shift+F9 or Debug > Delete All Breakpoints, not just manually. Just to make sure you don't have a breakpoint with a condition like `IncrementalCB = null` or something. – R. Martinho Fernandes Apr 16 '11 at 22:09
  • give us declaration of IncrementalCB. What it is? looks like property, so show us how it works – Marek Kwiendacz Apr 16 '11 at 22:20
  • @MarekKwiendacz is a Windows Forms Checkbox here is the documentation http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.aspx – mjsr Apr 16 '11 at 22:24
  • yes, i know, but how you declared it? IncrementalCB is rather property than local variable (incrementalCB) – Marek Kwiendacz Apr 16 '11 at 22:25

2 Answers2

2

It looks like IncrementalCB is null. Has it been initialized when you try to execute the code?

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
1

It looks like the CheckState variable is null when you are trying to access its Checked property:

CheckState.Checked
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @voodoomsr, if the code satisfied the if condition and crashed because `CheckState` was null I wonder how it will ever reach the last line. – Darin Dimitrov Apr 16 '11 at 21:55