退出应用程序时组合框的空指针 [英] Null pointers for comboboxes when exiting application

查看:150
本文介绍了退出应用程序时组合框的空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中调用Application.Exit时,我得到了nullpointerexception.组合框选定的索引设置为null,但是在退出过程中调用SelectedIndex_changed()时,这会引发一系列错误. :sigh:调用"SelectedIndex = -1"语句时,也会出现错误-再次发生在SelectedIndex_changed()中的nullPointerExceptions.我该如何处理?

I''m getting nullpointerexceptions when I call Application.Exit in C#. The combobox selected index is set to null, but this sets off a series of errors when SelectedIndex_changed() is called during the exit. :sigh: I also get errors when the "SelectedIndex = -1" statement is called--again nullPointerExceptions in SelectedIndex_changed(). How should I handle this? Thanks in advance.

推荐答案

首先,您如何将ComboBox.SelectedIndex设置为null?

它是一个int变量,它是不可为null的类型.

如果将其设置为null,那就是您的问题.

如果将其设置为-1,然后在SelectedIndex_Changed事件中出现错误,则需要首先检查SelectedIndex = -1并退出该方法.
First of all, how in the world are you setting ComboBox.SelectedIndex to null?

It is an int variable which is a non-nullable type.

If you''re setting it to null, then that''s your problem.

And if you''re setting it to -1 and then getting an error in your SelectedIndex_Changed event, then you need to first check to see if SelectedIndex = -1 and exit the method.


检查SelectedIndex的值,如果它为null或-1,则跳过错误的代码.
Check the value of SelectedIndex and skip over the code that errors if it''s null or -1.


或者,创建一个表单级别布尔值以指示该表单正在关闭并进行设置.在表单的关闭事件中为true.然后,如果该变量为false,则仅在组合框的已更改事件处理程序中运行代码.
Alternatively, create a form level boolean to indicate that the form is closing and set it to true in the form''s closing event. Then only run the code in your combo box''s changed event handler if that variable is false.


这篇关于退出应用程序时组合框的空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆