0

I was trying to read a xml configuration file like this:

public class ProductDb
{
    public ObservableCollection<Product> GetProducts()
    {
        ObservableCollection<Product> _products = new ObservableCollection<Product>();
        XDocument doc = XDocument.Load(@".\Config\Chip.xml");
        foreach(XElement productRow in doc.Root.Elements("Chip"))
        {
            var m = new Product(productRow.Element("ChipName").Value, productRow.Element("Series").Value, 
                productRow.Element("Type").Value,Convert.ToUInt32(productRow.Element("FlashAddress").Value), 
                Convert.ToUInt32(productRow.Element("PageCount").Value), Convert.ToUInt32(productRow.Element("PageSize").Value),
                Convert.ToUInt32(productRow.Element("RamAdd").Value), Convert.ToUInt32(productRow.Element("RamLength").Value));
            _products.Add(m);
        }
        return _products;
    }
}

The Class of Product is defined like this:

private string _chipName;
public string ChipName
{
    get { return _chipName; }
    set
    {
        _chipName = value;
        NotifyPropertyChanged("ChipName");
    }
}

private string _series;
public string Series{  ... }

private string _type;
public string Type{ ... }

private UInt32 _flashAddress;
public UInt32 FlashAddress{ ... }

private UInt32 _pageCount;
public UInt32 PageCount{ ... }

private UInt32 _pageSize;
public UInt32 PageSize{ ... }

private UInt32 _ramAddress;
public UInt32 RamAddress{ ... }

private UInt32 _ramLength;
public UInt32 RamLength{ ... }



public Product() { }

private string FlashInfo;
private string RamInfo;

public Product(string _chipName, string _series, string _type, UInt32 _flashAddress, 
               UInt32 _pageCount, UInt32 _pageSize,UInt32 _ramAddress, UInt32 _ramLength)
{
    ChipName = _chipName;
    Series = _series;
    Type = _type;

    FlashInfo = Convert.ToString(_flashAddress, 16).ToUpper().PadLeft(8, '0') + "---0x" +
        Convert.ToString((_flashAddress + _pageCount * _pageSize - 1), 16).ToUpper().PadLeft(8, '0') +
        "(" + Convert.ToString((_pageCount * PageSize / 1024), 10) + ")KB";

    RamInfo = Convert.ToString(_ramAddress, 16).ToUpper().PadLeft(8, '0') + "---0x" +
        Convert.ToString((_ramAddress + _ramLength - 1), 16).ToUpper().PadLeft(8, '0') + "(" +
        Convert.ToString((_ramLength / 1024), 10) + ")KB";
}

When I trying to get this by :

public ObservableCollection<Product> ProductsList { get; set; }
ProductDb pd = new ProductDb();
private void GetProductsList()
{
    try
    {
        ProductsList = new ObservableCollection<Product>(pd.GetProducts());
    }
     catch
    {
    }
}

It catches the Exception that "Object reference not set to an instance of an object." How should I fix this? Thanks in advance!

--------------------------Update--------------------------------------- The Xml file:

<System>
    <Chip>
       <ChipName>Hxxxxxxxx</ChipName>
       <Series>CM0+</Series>
       <Type>0</Type>
       <FlashAddress>00000000</FlashAddress>
       <PageCount>256</PageCount>
       <PageSize>512</PageSize>
       <RAMAddress>20000000</RAMAddress>
       <RAMLength>1800</RAMLength>
   </Chip>
</System>
BarryLib
  • 299
  • 1
  • 5
  • 20

0 Answers0