المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : nullrefererence exception



C# Programming
11-10-2009, 10:40 PM
Here is a block of code that copies a file:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string[] src = e.Argument as string[];
cnt = 0;
while (cnt < src.Length && src[cnt] != null)
{
toolStripStatusLabel1.Text = "Copying " + src[cnt] + "...";
string[] words = src[cnt].Split('\\');
string dest = keeppath2 + "\\" + words[words.Length - 1];
if (dest.IndexOf(".") > -1)
{
FileStream inFile = new FileStream(src[cnt].ToString(), System.IO.FileMode.Open);
if (!System.IO.File.Exists(dest))
{
using (System.IO.FileStream fs = System.IO.File.Create(dest))
{
for (byte i = 0; i < 100; i++)
{
fs.WriteByte(i);
}
}
}
FileStream outFile = new FileStream(dest, System.IO.FileMode.Open);
long size = inFile.Length;
int lineSize = 1024 * 64;
long filesize1 = inFile.Position / 1024;
long filesize2 = inFile.Length / 1024;
byte[] buffer = new byte[lineSize];
while ((inFile.Read(buffer, 0, lineSize)) > 0)
{
outFile.Write(buffer, 0, lineSize);
filesize1 = inFile.Position / 1024;
toolStripStatusLabel1.Text = "Copying " + src[cnt] + "... " + filesize1 + "KB of " + filesize2 + "KB";
}
outFile.Flush();
outFile.Close();
inFile.Flush();
inFile.Close();
}
else
{
try
{
copyDirectory(src[cnt].ToString(), dest, toolStripStatusLabel1);
}
catch (Exception error)
{
//toolStripStatusLabel1.Text = error.Message.ToString();
}
}
cnt++;
}
}

Every once in a while I get this error when copying:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.ToolStripItem.get_ContentRectangle()
at System.Windows.Forms.ToolStripSystemRenderer.RenderLabelInternal(ToolStripItemRenderEventArgs e)
at System.Windows.Forms.ToolStripSystemRenderer.OnRenderToolStripStatusLabelBackground(ToolStripItemRen derEventArgs e)
at System.Windows.Forms.ToolStripRenderer.DrawToolStripStatusLabelBackground(ToolStripItemRenderEventAr gs e)
at System.Windows.Forms.ToolStripStatusLabel.OnPaint(PaintEventArgs e)
at System.Windows.Forms.ToolStripItem.HandlePaint(PaintEventArgs e)
at System.Windows.Forms.ToolStrip.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.StatusStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNative********WndProc(Message& m)
at System.Windows.Forms.Native********Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
----------------------------------------
Filemon
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/Grimmstede/AppData/Local/Apps/2.0/GZ0XOYX1.QBL/HKQL7318.DOJ/file..tion_72d0129a53d9859f_0001.0000_fd31371f1e135068/Filemon.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:


"true" />


When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Does anyone know how to avoid this error?

I will also occasionally get this error:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.ToolStripItemCollection.get_Item(Int32 index)
at System.Windows.Forms.ToolStrip.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.StatusStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNative********WndProc(Message& m)
at System.Windows.Forms.Native********Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
----------------------------------------
Filemon
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/Grimmstede/AppData/Local/Apps/2.0/GZ0XOYX1.QBL/HKQL7318.DOJ/file..tion_72d0129a53d9859f_0001.0000_fd31371f1e135068/Filemon.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:


"true" />


When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.




Thanks for looking