2014-11-06 3 views
-1

Ошибка возникает при изменении размера массива только на одном конкретном массиве и при запуске программы за пределами идеи VS. Когда я нажимаю компиляцию и запускаю, ошибок не происходит.Ошибка появляется только при запуске программы как самостоятельная. C#

Вот деталь «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; 
} 

Это не случилось со мной, и я не думал, что это возможно.

+0

Что такое ZBuffer установлен? Если у вас просто есть Array zbuffer; это может быть вашей проблемой. – Robert

+1

'arr' должно быть нулевым - посмотрите код в' MAINW_Load' перед вызовом этой функции и посмотрите, как она может быть нулевой. –

+0

Возможный дубликат [Что такое исключение NullReferenceException и как его исправить?] (Http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason

ответ

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; 
} 
Смежные вопросы