仅在将程序作为独立的C#运行时出现错误

时间:2014-11-06 18:48:40

标签: c# arrays debugging

仅在一个特定阵列上调整数组大小时以及在VS构思之外运行程序时发生错误。当我点击编译并运行时,不会出现任何描述错误。

以下是'exeption'的详细信息

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 BWING.MAINW.ResizeArray(Array arr, Int32[] newSizes) in c:\Users\FFA\Documents\BWING\ENG\BWING\BWING\MAINW.cs:line 583
   at BWING.MAINW.MAINW_Load(Object sender, EventArgs e) in c:\Users\FFA\Documents\BWING\ENG\BWING\BWING\MAINW.cs:line 145
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
BWING
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/FFA/Documents/BWING/ENG/BWING/BWING/bin/Debug/BWING.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.17929 built by: FX45RTMREL
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.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:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

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.

以下是用于调整数组大小的代码:

ResizeArray(zbuffer, new int[] {xdisplay});

以下是我从互联网上获取的调整数组大小的方法:

private static Array ResizeArray(Array arr, int[] newSizes)
{
    if (newSizes.Length != arr.Rank)
        throw new ArgumentException("arr must have the same number of dimensions " +
                                    "as there are elements in newSizes", "newSizes");

    var temp = Array.CreateInstance(arr.GetType().GetElementType(), newSizes);
    int length = arr.Length <= temp.Length ? arr.Length : temp.Length;
    Array.ConstrainedCopy(arr, 0, temp, 0, length);
    return temp;
}

这从未发生在我身上,我认为不可能。

1 个答案:

答案 0 :(得分:0)

尝试添加以下行:

private static Array ResizeArray(Array arr, int[] newSizes)
{
    // Add next four lines
    if (newSizes == null)
        throw new ArgumentNullException("newSizes is null!"); 
    if (arr == null)
        throw new ArgumentNullException("arr is null!"); 
    if (newSizes.Length != arr.Rank)
        throw new ArgumentException("arr must have the same number of dimensions " +
                                    "as there are elements in newSizes", "newSizes");

    var temp = Array.CreateInstance(arr.GetType().GetElementType(), newSizes);
    int length = arr.Length <= temp.Length ? arr.Length : temp.Length;
    Array.ConstrainedCopy(arr, 0, temp, 0, length);
    return temp;
}