更改标签文本错误 C#
我的程序因此警告而停止工作
WindowsFormsApplication10.exe 中发生“System.NullReferenceException”类型的未处理异常
附加信息:对象引用未设置为实例 对象。
这是它停止的代码:
string stripStatusL = "some text: " + keepValues[lastTaken].ToString();
toolStripStatusLabel1.Text = stripStatusL;
它只是一个带有标签的 StatusStrip。我正在尝试更改该标签的文本。通常它适用于没有 StatusStrip 的标签。我的错误是什么?
视觉工作室2010 C#
My program stops working with this warning
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication10.exe
Additional information: Object reference not set to an instance of an
object.
Here is the code where it stops:
string stripStatusL = "some text: " + keepValues[lastTaken].ToString();
toolStripStatusLabel1.Text = stripStatusL;
It's just a StatusStrip with Label in. I'm trying to change the text of that Label. Usually it works for Label without StatusStrip. What is my mistake?
Visual Studio 2010
C#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我最好的猜测是
keepValues
是null
或lastTaken
索引/键(您的代码没有说明它是列表还是字典)集合中不存在。通常的原因是表单设计者可能无法在初始化时传入外部数据。表单有一个DesignMode
属性,如果表单在设计器中运行,则该属性为 true,您可以根据需要使用该属性提供一些模拟数据。My best guess would be that
keepValues
isnull
or thatlastTaken
index/key (your code doesn't say whether it's a list or dictionary) does not exist in the collection. The usual reason for that is that the forms designer might not be able to pass in external data on initialization. Forms have aDesignMode
property which is true if the form is run in the designer which you can use to provide some mock data if required.我猜
keepValues[lastTaken]
为空。所以
keepValues[lastTaken].ToString();
会给你一个 NullReferenceException。我建议使用调试器单步执行程序,并检查哪个对象为空
I'd guess
keepValues[lastTaken]
is null.so
keepValues[lastTaken].ToString();
will give you a NullReferenceException.I'd suggest stepping through the program with the debugger, and check which object is null
NullReferenceException 表示该实例为空。通过访问 null 实例,您将得到 NullReferenceException。确保该控件不为空。只需在将鼠标悬停在该行上设置一个断点,您就会看到出了什么问题。
NullReferenceException means that the instance is null. By accessing a null-instance, you get the NullReferenceException. Make sure that the control is not null. Just set a breakpoint at the line on hover over it and you'll see whats wrong.
keepValues
集合不包含等于lastTaken
的键,或者keepValues[lastTaken]
存在但其值为 nullkeepValues
collection does not contain a key equal tolastTaken
, orkeepValues[lastTaken]
exists but its value is null这将创建异常
因此,请记住用于从数组访问值的数组和索引的长度。
This will create the exception
So keep in mind the length of array and index you are using to access value from array.