.Net BinaryFormater System.ArgumentNullException: 对象图不能为空。参数名称:图形

.Net BinaryFormater System.ArgumentNullException: Object Graph cannot be null. Parameter name: graph

我正在尝试序列化 System.Collections.Generic.List 个对象,但是 运行 出现了一个我无法弄清楚的错误。

我尝试序列化的对象非常普通,只有很多字符串和整数属性。

我正在使用以下代码序列化对象列表。

    private static Boolean WriteItemDataList(List<ItemData> itemDataList)
    {
        try
        {
            using (Stream stream = File.Open(@"H:.cache", FileMode.Create))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(stream, itemDataList);
            }
        }
        catch (Exception exception)
        {
            Trace.WriteLine(exception.ToString());

            return false;
        }

        return true;
    }

我遇到了这个错误。

A first chance exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
System.ArgumentNullException: Object Graph cannot be null.
Parameter name: graph
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)   
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)   
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)   
at A.B.C.Program.WriteItemDataList(List`1 itemDataList) in z:\dev\Projects\\Program.cs:line 46

这是我要序列化的列表中的对象

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace A.B.ClassLibrary
{
[Serializable()]
public class ItemData
{
    //FIELDS
    protected String abcCode;

    protected Decimal averageCost;

    protected String basicUnitOfMeasure;

    protected Int32 basicUnitOfMeasureOrderQuantity;

    protected String businessArea;

    protected String businessAreaDescription;

    protected String buyer;

    protected Decimal concraftsPrice;

    protected String description;

    protected String domesticImport;

    protected Decimal dpPrice;

    protected String itemGroup;

    protected String itemGroupDescription;

    protected String itemNumber;

    protected String itemType;

    protected String itemTypeDescription;

    protected String name;

    protected String nameOfUserResponsible;

    protected Decimal netPrice;

    protected Int32 onOrderQuantity;

    protected String planner;

    protected String prePrice;

    protected Decimal prizmPrice;

    protected String procurementGroup;

    protected String procurementGroupDescription;

    protected String procureMethod;

    protected String productGroup;

    protected String productGroupDescription;

    protected Int32 purchaseMinimumQuantity;

    protected Int32 purchaseMultipleQuantity;

    protected String purchaseUnitOfMeasure;

    protected Decimal quantityBreakPrice;

    protected Int32 quantityBreakQuantity;

    protected Int32 reservedQuantity;

    protected String responsible;

    protected Decimal retailPrice;

    protected String royalty;

    protected String salesPriceUnitOfMeasure;

    protected String seasonality;

    protected Int32 sellMinimumQuantity;

    protected String sellUnitOfMeasure;

    protected String status;

    protected String supplierItemNumber;

    protected String supplierName;

    protected String supplierNumber;

    protected String topCust;

    protected Int32 totalLeadTime;

    protected String upc;

    protected Int32 warehouseOnHandQuantity;


    //PROPERTIES
    public virtual String AbcCode
    {
        get { return abcCode; }

        set { abcCode = value; }
    }

    public virtual Decimal AverageCost
    {
        get { return averageCost; }

        set { averageCost = value; }
    }

    public virtual String BasicUnitOfMeasure
    {
        get { return basicUnitOfMeasure; }

        set { basicUnitOfMeasure = value; }
    }

    public virtual Int32 BasicUnitOfMeasureOrderQuantity
    {
        get { return basicUnitOfMeasureOrderQuantity; }

        set { basicUnitOfMeasureOrderQuantity = value; }
    }

    public virtual String Buyer
    {
        get { return buyer; }

        set { buyer = value; }
    }

    public virtual String BusinessArea
    {
        get { return businessArea; }

        set { businessArea = value; }
    }

    public virtual String BusinessAreaDescription
    {
        get { return businessAreaDescription; }

        set { businessAreaDescription = value; }
    }

    public virtual Decimal ConcraftsPrice
    {
        get { return concraftsPrice; }

        set { concraftsPrice = value; }
    }

    public virtual String Description
    {
        get { return description; }

        set { description = value; }
    }

    public virtual String DomesticImport
    {
        get { return domesticImport; }

        set { domesticImport = value; }
    }

    public virtual Decimal DpPrice
    {
        get { return dpPrice; }

        set { dpPrice = value; }
    }

    public virtual String ItemGroup
    {
        get { return itemGroup; }

        set { itemGroup = value; }
    }

    public virtual String ItemGroupDescription
    {
        get { return itemGroupDescription; }

        set { itemGroupDescription = value; }
    }

    public virtual String ItemNumber
    {
        get { return itemNumber; }

        set { itemNumber = value; }
    }

    public virtual String ItemType
    {
        get { return itemType; }

        set { itemType = value; }
    }

    public virtual String ItemTypeDescription
    {
        get { return itemTypeDescription; }

        set { itemTypeDescription = value; }
    }

    public virtual String Name
    {
        get { return name; }

        set { name = value; }
    }

    public virtual String NameOfUserResponsible
    {
        get { return nameOfUserResponsible; }

        set { nameOfUserResponsible = value; }
    }

    public virtual Decimal NetPrice
    {
        get { return netPrice; }

        set { netPrice = value; }
    }


    public virtual Int32 OnOrderQuantity
    {
        get { return onOrderQuantity; }

        set { onOrderQuantity = value; }
    }

    public virtual String Planner
    {
        get { return planner; }

        set { planner = value; }
    }

    public virtual String PrePrice
    {
        get { return prePrice; }

        set { prePrice = value; }
    }

    public virtual Decimal PrizmPrice
    {
        get { return prizmPrice; }

        set { prizmPrice = value; }
    }

    public virtual String ProcurementGroup
    {
        get { return procurementGroup; }

        set { procurementGroup = value; }
    }

    public virtual String ProcurementGroupDescription
    {
        get { return procurementGroupDescription; }

        set { procurementGroupDescription = value; }
    }

    public virtual String ProcureMethod
    {
        get { return procureMethod; }

        set { procureMethod = value; }
    }

    public virtual String ProductGroup
    {
        get { return productGroup; }

        set { productGroup = value; }
    }

    public virtual String ProductGroupDescription
    {
        get { return productGroupDescription; }

        set { productGroupDescription = value; }
    }

    public virtual Int32 PurchaseMinimumQuantity
    {
        get { return purchaseMinimumQuantity; }

        set { purchaseMinimumQuantity = value; }
    }

    public virtual Int32 PurchaseMultipleQuantity
    {
        get { return purchaseMultipleQuantity; }

        set { purchaseMultipleQuantity = value; }
    }

    public virtual String PurchaseUnitOfMeasure
    {
        get { return purchaseUnitOfMeasure; }

        set { purchaseUnitOfMeasure = value; }
    }

    public virtual Decimal QuantityBreakPrice
    {
        get { return quantityBreakPrice; }

        set { quantityBreakPrice = value; }
    }

    public virtual Int32 QuantityBreakQuantity
    {
        get { return quantityBreakQuantity; }

        set { quantityBreakQuantity = value; }
    }

    public virtual Int32 ReservedQuantity
    {
        get { return reservedQuantity; }

        set { reservedQuantity = value; }
    }

    public virtual String Responsible
    {
        get { return responsible; }

        set { responsible = value; }
    }

    public virtual Decimal RetailPrice
    {
        get { return retailPrice; }

        set { retailPrice = value; }
    }

    public virtual String Royalty
    {
        get { return royalty; }

        set { royalty = value; }
    }

    public virtual String Seasonality
    {
        get { return seasonality; }

        set { seasonality = value; }
    }

    public virtual Int32 SellMinimumQuantity
    {
        get { return sellMinimumQuantity; }

        set { sellMinimumQuantity = value; }
    }

    public virtual String SellUnitOfMeasure
    {
        get { return sellUnitOfMeasure; }

        set { sellUnitOfMeasure = value; }
    }

    public virtual String Status
    {
        get { return status; }

        set { status = value; }
    }

    public virtual String SupplierItemNumber
    {
        get { return supplierItemNumber; }

        set { supplierItemNumber = value; }
    }

    public virtual String SupplierName
    {
        get { return supplierName; }

        set { supplierName = value; }
    }

    public virtual String SupplierNumber
    {
        get { return supplierNumber; }

        set { supplierNumber = value; }
    }

    public virtual String TopCust
    {
        get { return topCust; }

        set { topCust = value; }
    }

    public virtual Int32 TotalLeadTime
    {
        get { return totalLeadTime; }

        set { totalLeadTime = value; }
    }

    public virtual String UPC
    {
        get { return upc; }

        set { upc = value; }
    }

    public virtual Int32 WarehouseOnHandQuantity
    {
        get { return warehouseOnHandQuantity; }

        set { warehouseOnHandQuantity = value; }
    }



    //INITIALIZE
    public ItemData()
    {
        abcCode = null;

        averageCost = 0;

        basicUnitOfMeasure = null;

        basicUnitOfMeasureOrderQuantity = 0;

        buyer = null;

        businessArea = null;

        businessAreaDescription = null;

        concraftsPrice = 0;

        description = null;

        dpPrice = 0;

        domesticImport = null;

        itemGroup = null;

        itemGroupDescription = null;

        itemNumber = null;

        itemType = null;

        itemTypeDescription = null;

        name = null;

        nameOfUserResponsible = null;

        netPrice = 0;

        onOrderQuantity = 0;

        planner = null;

        prePrice = null;

        prizmPrice = 0;

        procurementGroup = null;

        procurementGroupDescription = null;

        procureMethod = null;

        productGroup = null;

        productGroupDescription = null;

        purchaseMinimumQuantity = 0;

        purchaseMultipleQuantity = 0;

        purchaseUnitOfMeasure = null;

        quantityBreakPrice = 0;

        quantityBreakQuantity = 0;

        reservedQuantity = 0;

        responsible = null;

        retailPrice = 0;

        royalty = null;

        seasonality = null;

        sellMinimumQuantity = 0;

        sellUnitOfMeasure = null;

        status = null;

        supplierItemNumber = null;

        supplierName = null;

        supplierNumber = null;

        topCust = null;

        totalLeadTime = 0;

        upc = null;

        warehouseOnHandQuantity = 0;
    }
}

}

BinaryFormatter.Serialize 的文档指出,如果 serializationStreamgraph 参数中的任何一个是 null,它将抛出 ArgumentNullException。由于消息明确指出 "Object graph cannot be null",这意味着 graph 参数是 null,因此 WriteItemDataList 方法的 itemDataList 参数必须是 null .