Matthew
Matthew

Reputation: 105

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

I'm attempting to serialize a System.Collections.Generic.List of objects but am running into an error I can't figure out.

The objects I am trying to serialize are pretty vanilla, just a lot of strings and integers properties.

I'm using the following code to serialize a list of objects.

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

            return false;
        }

        return true;
    }

I am getting this error.

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

This is the object in the list I'm trying to serialize

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

}

Upvotes: 1

Views: 7465

Answers (1)

juharr
juharr

Reputation: 32266

The documentation for BinaryFormatter.Serialize states that it will throw an ArgumentNullException if either of the serializationStream or graph parameters are null. Since the message specifically says "Object graph cannot be null" that means that the graph parameter is null, so the itemDataList parameter to your WriteItemDataList method must be null.

Upvotes: 2

Related Questions