最新消息:20210917 已从crifan.com换到crifan.org

【已解决】C#程序出错:Object reference not set to an instance of an object

C# crifan 53522浏览 0评论

【背景】

一个C#程序,利用Amazon的AWS的API去获得对应的Amazon产品的信息。

结果运行期间出错:

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

System.NullReferenceException: Object reference not set to an instance of an object.

   at ScrapeAmazonProduct.frmScrapeAmazonProduct.awsGetAllProductInfo(String itemAsin, awsItemAttributes itemAttributes)

   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)

对应最后的log信息是:

[2013-06-19 19:13:53][ INFO] =========================== [850] Valid: B0007S5N8O ============================

[2013-06-19 19:13:53][DEBUG] Extracting info for: B0007S5N8O

[2013-06-19 19:13:53][ INFO] [Title] 9 Kenmore 50558, 5055, 50557 Micro Filtration Canister Vacuum Bags

[2013-06-19 19:13:53][ INFO] [Description] Panasonic and Sears Kenmore Vacuum Clean ……

[2013-06-19 19:13:53][ INFO] [BulletList] Total 1 bullets

[2013-06-19 19:13:53][DEBUG] not find EditorialReviews for ASIN=B0007S5N8O

【解决过程】

1.很明显,是哪里返回的空值,null值,导致出现:

Object reference not set to an instance of an object

的错误的。

2.所以就去看了看awsGetAllProductInfo部分的代码,找找是哪里返回的null。

同时,去看了看:

not find EditorialReviews for

所对应的代码的部分,是函数

public awsEditorialReview awsGetEditorialReview(string itemAsin)

中的。

3.但是,结果去测试后发现,对于上述的B0007S5N8O,是可以正常返回对应的EditorialReviews的。

4.所以后来推测是:

之前通过正常的模式,利用HttpWebRequest和HttpWebResponse去获得一个url的html。

后来为了不阻塞UI线程,利用了BackgroundWorker去做这个事情。

以为是:

有时候,通过BackgroundWorker,利用HttpWebRequest和HttpWebResponse,所返回的html,会错乱掉。

比如正常的是:

url1获得是html1,然后url2获得是html2

但是有时候调用BackgroundWorker多次,或者是频率太高了?

会出现,访问url1但返回html2的情况,导致后续代码出错。

5.但是后来的结果证明,实际上,是由于之前的代码:

public awsImages awsGetImages(string itemAsin)

中,也是有上述的:

not find EditorialReviews for

而实际上出错的,是上面的这个awsGetImages。

对应的B0007S5N8O返回的内容中,的确没有包含对应的Images:

<?xml version="1.0"?>
<ItemLookupResponse>
    <OperationRequest>
        <HTTPHeaders>
            <Header Name="UserAgent" Value="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E"/>
        </HTTPHeaders>
        <RequestId>4878a911-d0ac-472b-9077-a3595a1773f4</RequestId>
        <Arguments>
            <Argument Name="Operation" Value="ItemLookup"/>
            <Argument Name="Service" Value="AWSECommerceService"/>
            <Argument Name="Signature" Value="Fc7pdsPwmLFafceDEbRz2WlDW69TWoPtBbLqLw7lLfg="/>
            <Argument Name="AssociateTag" Value="xxx"/>
            <Argument Name="Version" Value="2011-08-01"/>
            <Argument Name="ItemId" Value="B0007S5N8O"/>
            <Argument Name="IdType" Value="ASIN"/>
            <Argument Name="AWSAccessKeyId" Value="xxx"/>
            <Argument Name="Timestamp" Value="2013-06-20T04:52:21Z"/>
            <Argument Name="ResponseGroup" Value="Images"/>
        </Arguments>
        <RequestProcessingTime>0.0091180000000000</RequestProcessingTime>
    </OperationRequest>
    <Items>
        <Request>
            <IsValid>True</IsValid>
            <ItemLookupRequest>
                <IdType>ASIN</IdType>
                <ItemId>B0007S5N8O</ItemId>
                <ResponseGroup>Images</ResponseGroup>
                <VariationPage>All</VariationPage>
            </ItemLookupRequest>
        </Request>
        <Item>
            <ASIN>B0007S5N8O</ASIN>
        </Item>
    </Items>
</ItemLookupResponse>

 

【总结】

教训是:

1.以后写代码,还是要更加仔细,避免拷贝参考别的代码时,没有修改对应的部分的内容,而导致以后可能发生其他的问题。

比如此处在写awsGetImages的代码时,参考了awsGetEditorialReview,结果导致找不到Images节点时报错的信息,却是awsGetEditorialReview的

not find EditorialReviews for

而导致错误原因误判了,导致后续的调试困难。

幸亏后来发现及时,否则真不知道如何解决此问题了。

经验是:

1.在出错,异常的地方,该加的代码,要是尽量不要省,否则,却的时间,最后总会被bug所“补”上来的。。

转载请注明:在路上 » 【已解决】C#程序出错:Object reference not set to an instance of an object

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.207 seconds, using 23.35MB memory