locked
list<customlist> is populated but cannot be built to be sent. RRS feed

  • Question

  • User-1246222823 posted

    Hi all,

    I am developing a web service to return a list and following code working properly until adding line. The error message is "Object reference not found...."

    declaring variables :

    public class ticketinfo
            {
                public int ticketno{ get; set; }
                public string ticketcode { get; set; }
                public DateTime createdate { get; set; }
                public string details{ get; set; }
                public string location{ get; set; }
                public short status { get; set; }
            }
    
            public class myticketslist
            {
                public List<ticketinfo> tickets;
            }

    and the function

            [WebMethod]
            public myticketlist retrieveData(DateTime start, int stats)
            {
                myticketlist myresult= new myticketlist();
                mydataset= tf.sqlquery("..........any select query here..........", "a");
                if (mydataset!= null)
                    if (mydataset.Tables[0].Rows.Count > 0)
                    {
                        myticketlist temptic= new myticketlist();
                        foreach (DataRow r in mydataset.Tables[0].Rows) //(there are many rows)
                        {
                            ticketinfo i = new ticketinfo();
                            i.ticketno= Convert.ToInt32(r.ItemArray[0].ToString());
                            i.ticketcode= r.ItemArray[1].ToString();
                            i.createdate= Convert.ToDateTime(r.ItemArray[2]);
                            i.details= r.ItemArray[3].ToString();
                            i.location= r.ItemArray[4].ToString();
                            i.status= Convert.ToInt16(r.ItemArray[5]);
                            temptic.tickets.Add(i); // <<< it returns error here
                        }
                        myresult= temptic;
                    }
                return myresult;
            }

    it launches succesfully but returns following error on the marked line

    System.NullReferenceException: Object reference not set to an instance of an object.
    Thursday, April 18, 2013 9:13 AM

Answers

  • User-917364509 posted

    You need to create the list before adding any items to it.

    temptic.tickets = new List<ticketinfo>();
    temptic.tickets.Add(i); // <<< it returns error here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 9:18 AM
  • User1124521738 posted

    declare as property not variable - add getER and setER and init the property when instantiating the class.

    public class myticketslist {
    
    	public myticketslist() {
    		tickets = new List<ticketinfo>();
    	}
    
    	public List<ticketinfo> tickets { get; set; }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 9:45 AM

All replies

  • User166892907 posted

    what is the value of "myresult" there :

    return myresult;
    Thursday, April 18, 2013 9:17 AM
  • User-917364509 posted

    You need to create the list before adding any items to it.

    temptic.tickets = new List<ticketinfo>();
    temptic.tickets.Add(i); // <<< it returns error here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 9:18 AM
  • User1124521738 posted

    declare as property not variable - add getER and setER and init the property when instantiating the class.

    public class myticketslist {
    
    	public myticketslist() {
    		tickets = new List<ticketinfo>();
    	}
    
    	public List<ticketinfo> tickets { get; set; }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 18, 2013 9:45 AM