确定此错误的原因,Checkbox c#
大家好,我很难理解为什么我的一些代码崩溃了。有问题的指令是这样的:
第一次尝试:
bool aers = vm_SessionTab.IncrementalConstruction; // != null
bool chua = IncrementalCB.Checked; // != null
if (aers) IncrementalCB.CheckState = CheckState.Checked; // crash, null reference exception
第二次尝试:
bool aers = vm_SessionTab.IncrementalConstruction; // != null
bool chua = IncrementalCB.Checked; // != null
IncrementalCB.Checked = vm_SessionTab.IncrementalConstruction //crash, null reference exception
IncrementalCB 是一个 CheckBox。 vm_SessionTab 是一个个性化对象,其中包含我想要显示的状态。 当我使用调试器时,我发现“aers”和“chua”与空不同。然后在接下来的两次尝试中,程序崩溃告诉我它发生了空引用异常。我想问这是怎么发生的。我明确检查这两个值都不同于 null,:S。完整的代码相当大,但这些是相关的行。
更多信息:
我真的确定 IncrementalCB 是 != null 但如果我在我显示的代码之前创建另一个复选框,则不会引发异常并且控件会正确显示。如果控件已经存在,为什么我需要重新创建控件? 这有效:
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;
编辑添加...
谢谢大家建议我查看堆栈跟踪。从中我看到了我所犯的可怕错误。在伪代码解释中,这是我的错误:(我尝试应用 MVVP 模式)
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
因此,当要求尚未完成的演示者完成更改时,结果是空引用异常。由于我无法访问内部 Checked 字段来进行直接更改,而无需引发事件 Checked_Changed,因此我需要移出回调。 该错误有点难以看到它,因为当我在调试器中使用 f11 跳入时,没有显示该事件的事件处理程序的调用,所以我只能看到每个对象都以良好的方式执行的行他们的行动
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
IncrementalCB
是null
。当您尝试执行代码时它是否已初始化?It looks like
IncrementalCB
isnull
. Has it been initialized when you try to execute the code?当您尝试访问其
Checked
属性时,CheckState
变量看起来为 null:It looks like the
CheckState
variable is null when you are trying to access itsChecked
property: