1

I had a similar problem with a dictionary - now I'm trying to populate a viewmodel, to return a JSON object to a GET request.

My viewmodel is:

public class HotelInventoryta
{
    public int api_version { get; set; }
    public string lang { get; set; }
    public List<Hotel_List_ta> hotels { get; set; }
}

public class Hotel_List_ta
{
    public int ta_id { get; set; }
    public string partner_id { get; set; }  
    public string name { get; set; }   
    public string street { get; set; }  
    public string city { get; set; }    
    public string postal_code { get; set; } 
    public string state { get; set; }   
    public string country { get; set; }
    public double latitude { get; set; }    
    public double longitude { get; set; }  
    public string desc { get; set; }    
    public string url { get; set; }    
    public string email { get; set; }  
    public string phone { get; set; }   
    public string fax { get; set; }     
}

My DataBase model is:

 [Table("tblHotel")]
public class Hotelta
{
    [Key()]
    [Column("hotel_id")]
    public long hotel_id { get; set; }
    public string hotel_name { get; set; }
    public string hotel_add1 { get; set; }
    public string hotel_towncity { get; set; }
    public string hotel_pc { get; set; }
    public string hotel_country { get; set; }
    public string hotel_pass { get; set; }
    public string hotel_email { get; set; }
    public string hotel_tel { get; set; }
    public string hotel_fax { get; set; }
}

My controller code to populate the viewmodel is:

    private HoteltaContext dbh = new HoteltaContext();
    //
    // GET: /ta/hotel_inventory
    [HttpGet]
    public HotelInventoryta hotel_inventory(int api_version, string lang)
    {
        {

            HotelInventoryta hotelinventory = new HotelInventoryta();
            hotelinventory.api_version = api_version;
            hotelinventory.lang = lang;

            // Get data from database
            var h = dbh.Hotelta.Where(x => x.hotel_id != 0).ToList();

            // loop through each result, and add it to the hotelinventory.hotels model
            foreach (var ht in h)
            {
                // I get the exception on the next line
                hotelinventory.hotels.Add(new Hotel_List_ta
                {
                    ta_id = 0,
                    partner_id = ht.hotel_id.ToString(),
                    name = ht.hotel_name,
                    street = ht.hotel_add1,
                    city = ht.hotel_towncity,
                    postal_code = ht.hotel_pc,
                    country = ht.hotel_country,
                    url = "http://www.me.com",
                    email = ht.hotel_email,
                    phone = ht.hotel_tel,
                    fax = ht.hotel_fax
                });
            }

            return hotelinventory;
        }
    }

The error is:

Object reference not set to an instance of an object

Firstly, can you help me resolve the error - and if possible, confirm if the way I am reading from the database and populating the viewmodel, is the best way to do it?

Thank you, Mark

1
  • I think it would be very helpful if you show the full stack trace. Or if you at least tell at which row the exception occurs. Commented Nov 1, 2013 at 15:56

1 Answer 1

4

This is because the hotels property is never initialized. You could do this in the constructor of HotelInventoryta:

public class HotelInventoryta
{
    public HotelInventoryta()
    {
        hotels  = new List<Hotel_List_ta>();
    }

    // ...
}

Now you initialzed the property with an empty collection, so you can add items to it, rather than hotels being null which causes your exception.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.