当程序作为一个独立的C#运行时,只会出现错误

本文关键字:运行时 错误 独立 程序 一个 | 更新日期: 2023-09-27 18:29:22

当仅在一个特定数组上调整数组大小时,以及当程序在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;
}

这从来没有发生在我身上,我认为这是不可能的。

当程序作为一个独立的C#运行时,只会出现错误

尝试添加以下行:

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;
}