Settings - first chance exception of type 'System.NullReferenceException'

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im getting following exception : <font size=1>
A first chance exception of type System.NullReferenceException occurred in Copy DB.exe
When using the following code (bold part) : <font size=2>
</font><font color="#0000ff" size=2>private</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> cboDatabase_SelectedIndexChanged(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e)
{
</font><font color="#0000ff" size=2>string</font><font size=2> s = ((</font><font color="#008080" size=2>ComboBox</font><font size=2>)sender).Text.Substring(((</font><font color="#008080" size=2>ComboBox</font><font size=2>)sender).Text.IndexOf(</font><font color="#800000" size=2>"_"</font><font size=2>) + 1);
</font><font color="#0000ff" size=2>int</font><font size=2> i;
</font><font color="#0000ff" size=2>if</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2>.TryParse(s.Substring(s.IndexOf(</font><font color="#800000" size=2>"_"</font><font size=2>) + 1, 1), </font><font color="#0000ff" size=2>out</font><font size=2> i))
{
s = s.Substring(0, s.IndexOf(</font><font color="#800000" size=2>"_"</font><font size=2>));
}
</font><font color="#0000ff" size=2>else
</font><font size=2>{
s = s.Substring(0, s.IndexOf(</font><font color="#800000" size=2>"_"</font><font size=2>, s.IndexOf(</font><font color="#800000" size=2>"_"</font><font size=2>) + 1));
}
</font><font color="#0000ff" size=2>if</font><font size=2> (</font><font color="#008080" size=2>UserSettings</font><font size=2>.Default != </font><font color="#0000ff" size=2>null</font><font size=2>)
</font><font color="#0000ff" size=2>try
</font><font size=2>{
tbTargetDatabaseName.Text = </font><font color="#008080" size=2> UserSettings </font><font size=2> .Default.PropertyValues[_s_].PropertyValue.ToString();
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#008080" size=2>Exception</font><font size=2> ex)
{
</font><font color="#008080" size=2>Console</font><font size=2>.WriteLine(ex.Message);
tbTargetDatabaseName.Text = </font><font color="#008080" size=2>UserSettings</font><font size=2>.Default.Properties[_s_].DefaultValue.ToString();
}
</font><font color="#0000ff" size=2>else
</font><font size=2>tbTargetDatabaseName.Text = s;
SelectScripts(((</font><font color="#008080" size=2>ComboBox</font><font size=2>)sender).Text, </font><font color="#0000ff" size=2>ref</font><font size=2> clbScripts);
}</font>
<font size=2>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 "<font color="#008080 UserSettings</font> <font size=2> .Default.PropertyValues[_s_].PropertyValue" to the watch. Afterwards I can remove the breakpoint and trigger the event (time after time) without any issue.</font></font>
<font size=2>Anyone knows whats the reason for this behaviour ?</font>
<font size=2>ps, I used [_s_] as it became a moon-emoticon when using only s. </font></font>

View the full article
 
Back
Top