-1

I am attempting to retrieve information using the Steam API. I created the classes offerStuff and itemsClass, offerStuff contains public List<itemsClass> items { get; set; }, however, whenever I attempt to access this list through os.items.Add(item), my program crashes with NullReferenceException. Is there some declaration I am missing? If so, how would I declare it so I can access it without the exception?

    public static List<offerStuff> pollOffers()
    {
        using (dynamic tradeOffers = WebAPI.GetInterface("IEconService", config.API_Key))
        {
            List<offerStuff> OfferList = new List<offerStuff>();
            offerStuff os = new offerStuff();
            KeyValue kvOffers = tradeOffers.GetTradeOffers(get_received_offers: 1);//, active_only: 1
            foreach (KeyValue kv in kvOffers["trade_offers_received"].Children)
            {
                os.tradeofferid = kv["tradeofferid"].AsInteger(); ;
                os.accountid_other = Convert.ToUInt64(kv["accountid_other"].AsInteger());
                os.message = kv["message"].AsString();
                os.trade_offer_state = kv["trade_offer_state"].AsInteger();
                foreach (KeyValue kv2 in kv["items_to_receive"].Children)
                {
                    itemsClass item = new itemsClass();
                    item.appid = (kv2["appid"].AsInteger());
                    item.assetid = kv2["assetid"].AsInteger();
                    item.classid = kv2["classid"].AsInteger();
                    item.instanceid = kv2["instanceid"].AsInteger();
                    item.amount = kv2["amount"].AsInteger();
                    item.missing = kv2["missing"].AsInteger();

                    os.items.Add(item);
                }
                os.is_our_offer = kv["is_our_offer"].AsBoolean();
                os.time_created = kv["time_created"].AsInteger();
                os.time_updated = kv["time_updated"].AsInteger();
                OfferList.Add(os);
            }
            return OfferList;
        }
    }
}


public class offerStuff
{
    public int tradeofferid { get; set; }
    public SteamID accountid_other { get; set; }
    public string message { get; set; }
    public int trade_offer_state { get; set; }
    public List<itemsClass> items { get; set; }
    public bool is_our_offer { get; set; }
    public int time_created { get; set; }
    public int time_updated { get; set; }
}
public class itemsClass
{
    public int appid { get; set; }
    public int assetid { get; set; }
    public int classid { get; set; }
    public int instanceid { get; set; }
    public int amount { get; set; }
    public int missing { get; set; }
}
Chris Altig
  • 680
  • 3
  • 8
  • 22
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Blorgbeard Sep 11 '14 at 02:42

1 Answers1

4

The problem is probably that you're not initializing the collection items. You could do it on your contructor like this

public offerStuff() 
{
    items = new List<itemsClass>();
}
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155