设置-类型'System.NullReferenceException'的第一次机会异常 [英] Settings - first chance exception of type 'System.NullReferenceException'

查看:412
本文介绍了设置-类型'System.NullReferenceException'的第一次机会异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下异常:

复制DB.exe中发生了类型为'System.NullReferenceException'的第一次机会异常

使用以下代码(粗体部分)时:

私有 无效 cboDatabase_SelectedIndexChanged( object 发送者, EventArgs e)
{
string s =((( ComboBox )发送者).Text.Substring(((( 组合框 )发送方).Text.IndexOf( "__" )+ 1);
int i;
if ( int .TryParse(s.Substring(s.IndexOf( "__" )+ 1,1), out i))
{
s = s.Substring(0,s.IndexOf(
" _" )));
}
else
{
s = s.sstring(0,s.IndexOf(
"__" ,s.IndexOf( &; _" )+ 1));
}
如果 ( UserSettings .默认值== )
尝试
{
tbTargetDatabaseName.Text =
UserSettings .Default.PropertyValues[_s_] .PropertyValue.ToString();
}
catch ( 例外 例如)
{
控制台 .WriteLine(ex.Message);
tbTargetDatabaseName.Text =
UserSettings .Default.Properties [_s _].DefaultValue.ToString();
}
else
tbTargetDatabaseName.Text = s;
SelectScripts(((((
ComboBox )sender).Text, ref clbScripts);
}

每次我在控制台窗口中遇到Excepion时,当我在组合框中选择另一个项目时,就会触发此事件...除非我在该行上设置了断点(这会导致异常) ),然后添加" UserSettings .Default.PropertyValues[_s _].PropertyValue"" .之后,我可以删除断点并触发事件(一次又一次),而不会出现任何问题.

任何人都知道这种行为的原因是什么?

ps,我使用了 [_ s_] ,因为当仅使用s时,它变成了月亮图释.

解决方案

第一次更改异常意味着您有未处理的异常.您可以将try放在事件处理程序方法的开头,而不仅仅是将try放入一行,因此整个代码都将放在try结构中.

也尝试调试,也许属性get方法后面有一些代码无法执行.


I'm getting following exception :

A first chance exception of type 'System.NullReferenceException' occurred in Copy DB.exe

When using the following code (bold part) :

private void cboDatabase_SelectedIndexChanged(object sender, EventArgs e)
{
string s = ((ComboBox)sender).Text.Substring(((ComboBox)sender).Text.IndexOf("_") + 1);
int i;
if (int.TryParse(s.Substring(s.IndexOf("_") + 1, 1), out i))
{
s = s.Substring(0, s.IndexOf(
"_"));
}
else
{
s = s.Substring(0, s.IndexOf(
"_", s.IndexOf("_") + 1));
}
if (UserSettings.Default != null)
try
{
tbTargetDatabaseName.Text =
UserSettings.Default.PropertyValues[_s_].PropertyValue.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
tbTargetDatabaseName.Text =
UserSettings.Default.Properties[_s_].DefaultValue.ToString();
}
else
tbTargetDatabaseName.Text = s;
SelectScripts(((
ComboBox)sender).Text, ref clbScripts);
}

This event is firing when I select another item in my combobox, each time I end up with the Excepion in my console window... Unless I set a breakpoint on the line (which causes the exception) and add "UserSettings.Default.PropertyValues[_s_].PropertyValue" to the watch. Afterwards I can remove the breakpoint and trigger the event (time after time) without any issue.

Anyone knows what's the reason for this behaviour ?

ps, I used [_s_] as it became a moon-emoticon when using only s.

解决方案

first change exception means you have unhandled exception. Instead of just puting one line in try, you can put try at the begining of event handler method, so the whole code will be in try structure.

Also try to debug, maybe there is some code behind property get method that fails to execute.


这篇关于设置-类型'System.NullReferenceException'的第一次机会异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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