【已解决】C#程序出错:Array cannot be null

【背景】

写了个C#的exe程序。

本地测试Ok,拿到别处运行,结果出错:

Array cannot be null

错误详细信息:

See the end of this message for details on invoking 

just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************

System.ArgumentNullException: Array cannot be null.

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.createOutputFile(String excelFullFilename)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.saveProductInfo(AmazonProductInfo productInfo)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.processAmazonItem(String itemAsin)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.processAwsSearchItem(awsSearchResultItem singleAwsSearchItem)

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.awsMainCategorySearch()

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.btnSearch_Click(Object sender, EventArgs e)

   at System.Windows.Forms.Control.OnClick(EventArgs e)

   at System.Windows.Forms.Button.OnClick(EventArgs e)

   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

   at System.Windows.Forms.Control.WndProc(Message& m)

   at System.Windows.Forms.ButtonBase.WndProc(Message& m)

   at System.Windows.Forms.Button.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: 2.0.0.0

    Win32 Version: 2.0.50727.5466 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll

—————————————-

ScrapeAmazonProduct

    Assembly Version: 1.0.0.0

    Win32 Version: 3.1.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-06-17.exe

—————————————-

System.Windows.Forms

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5468 (Win7SP1GDR.050727-5400)

    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.5467 (Win7SP1GDR.050727-5400)

    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.5467 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

—————————————-

System.Configuration

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

—————————————-

System.Xml

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll

—————————————-

NLog

    Assembly Version: 2.0.0.0

    Win32 Version: 3.1.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-06-17.exe

—————————————-

System.ServiceModel

    Assembly Version: 3.0.0.0

    Win32 Version: 3.0.4506.5452 (Win7SP1GDR.030729-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.ServiceModel/3.0.0.0__b77a5c561934e089/System.ServiceModel.dll

—————————————-

System.Data

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll

—————————————-

System.Core

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5420 built by: Win7SP1

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll

—————————————-

System.Runtime.Serialization

    Assembly Version: 3.0.0.0

    Win32 Version: 3.0.4506.5452 (Win7SP1GDR.030729-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Serialization/3.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll

—————————————-

System.Web

    Assembly Version: 2.0.0.0

    Win32 Version: 2.0.50727.5456 (Win7SP1GDR.050727-5400)

    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Web/2.0.0.0__b03f5f7f11d50a3a/System.Web.dll

—————————————-

HtmlAgilityPack

    Assembly Version: 1.4.6.0

    Win32 Version: 3.1.0.0

    CodeBase: file:///C:/Users/Eli%20Shimony/Desktop/ScrapeAmazonProduct_2013-06-17.exe

—————————————-

System.Web.Extensions

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5446

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Web.Extensions/3.5.0.0__31bf3856ad364e35/System.Web.Extensions.dll

—————————————-

System.Web.Abstractions

    Assembly Version: 3.5.0.0

    Win32 Version: 3.5.30729.5420

    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Web.Abstractions/3.5.0.0__31bf3856ad364e35/System.Web.Abstractions.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.

 

【解决过程】

1.去看了下相关代码:

        private void createOutputFile(string excelFullFilename)
        {
            gLogger.Info("Creating ouput file " + excelFullFilename);

            bool isAutoFit = true;
            bool isHeaderBold = true;
            
            //init
            //if exist remove it
            if (File.Exists(excelFullFilename))
            {
                File.Delete(excelFullFilename);
            }

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            const int excelRowHeader = 1;
            const int excelColumnHeader = 1;
            
            //save header
            int curColumnIdx = 0 + excelColumnHeader;
            int rowIdx = 0 + excelRowHeader;

            xlWorkSheet.Cells[rowIdx, curColumnIdx++] = "Title";
            xlWorkSheet.Cells[rowIdx, curColumnIdx++] = "Description";
            const int constBullerLen = 5;
            for (int bulletIdx = 0; bulletIdx < constBullerLen; bulletIdx++)
            {
                int bulletNum = bulletIdx + 1;
                xlWorkSheet.Cells[rowIdx, curColumnIdx + bulletIdx] = "Bullet" + bulletNum.ToString();
            }
            curColumnIdx = curColumnIdx + constBullerLen;
            const int constImgNameListLen = 5;
            for (int imgIdx = 0; imgIdx < constImgNameListLen; imgIdx++)
            {
                int imgNum = imgIdx + 1;
                xlWorkSheet.Cells[rowIdx, curColumnIdx + imgIdx] = "ImageFilename" + imgNum.ToString();
            }
            curColumnIdx = curColumnIdx + constImgNameListLen;
            xlWorkSheet.Cells[rowIdx, curColumnIdx++] = "HighestPrice";
            xlWorkSheet.Cells[rowIdx, curColumnIdx++] = "OneSellerIsAmazon";
            xlWorkSheet.Cells[rowIdx, curColumnIdx++] = "ReviewNumber";
            xlWorkSheet.Cells[rowIdx, curColumnIdx++] = "IsBestSeller";

            //formatting
            //(1) header to bold
            if (isHeaderBold)
            {
                Range headerRow = xlWorkSheet.get_Range("1:1", System.Type.Missing);
                headerRow.Font.Bold = true;
            }
            //(2) auto adjust column width (according to content)
            if (isAutoFit)
            {
                Range allColumn = xlWorkSheet.Columns;
                allColumn.AutoFit();
            }

            //output
            xlWorkBook.SaveAs(excelFullFilename,
                                XlFileFormat.xlWorkbookNormal,
                                misValue,
                                misValue,
                                misValue,
                                misValue,
                                XlSaveAsAccessMode.xlExclusive,
                                XlSaveConflictResolution.xlLocalSessionChanges,
                                misValue,
                                misValue,
                                misValue,
                                misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            crl.releaseObject(xlWorkSheet);
            crl.releaseObject(xlWorkBook);
            crl.releaseObject(xlApp);
        }

想要找到其中的,到底是那个array的变量是null而导致此错误的。

然后看了半天,貌似也没有啥Array的变量。

2.然后后来才想起来,另外一个报错:

【已解决】虽然已经安装了Office的Excel但是C#的exe还是运行出错:Could not load file or assembly ‘Microsoft.Office.Interop.Excel, Version=14.0.0.0

是缺少了excel的dll库,所以,估计此处是这句:

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

或者是:

xlWorkSheet.Cells[rowIdx, curColumnIdx++] = "Title";

而导致出错的,猜测是:

get_Item返回的值是Array;

或者是:xlWorkSheet.Cells通过rowIdx所要取的值是Array的。

3.想要去查证看看api的:

XmlMappedRange.get_Item Method (2003 System)

public Object get_Item(
	Object RowIndex,
	Object ColumnIndex
)

以及:

_Worksheet.Cells Property

Range Cells { get; }

好像是,又好像不是。

4.但是不管了,先去解决前面的excel的dll库集成进去的问题,然后估计此问题就自然而然不存在了。

也就无需纠结于,到底是哪行代码产生的Array了。

【总结】

待后续验证,估计就是缺少了excel而导致出错的。

后来的结果验证了,的确就是缺少了excel的dll而出错的。

而在,解决这个问题:

【已解决】虽然已经安装了Office的Excel但是C#的exe还是运行出错:Could not load file or assembly ‘Microsoft.Office.Interop.Excel, Version=14.0.0.0

期间,已经集成了excel的dll了,所以就顺带解决了此处的问题了。


【题外话】

另外,注意到:

运行同样的程序

别人在别处,是第90个item是那个B000IDSLOG

而我此处是第97个item是那个B000IDSLOG:

97 item B000IDSLOG

此说明一个问题:

对于同样的代码,都是用的是Amazon的AWS接口

但是检测出来的内容,却是不一致的。

原因可能有:

1.其早上运行,和现在运行,期间,Amazon的产品更新了,所以检测结果不一样

2.要么是本身,别人所在的地方(别的国家,以色列?美国?),和我此处的地区(中国),所返回的结果不一致。

即Amazon对于同样的代码,但是对于物理上的,不同markerplace,所返回的结果,也还是不同的。

(虽然内部都是用的是US的地区是检索amazon的产品的)



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量