0

i am new to class/object.I am getting this error. I don't know why this is not navigating to the desired page. There is my code. Thanks for helping~~

WebService1.asmx.cs

public class yearList
{
    public yearList(){}
    public int year;
    public List<int> list;

    public List<int> List()
    {
         for (int i = -2; i < 3; i++)
        {
            list.Add(year+i);
        }
         return list;
    }
}

[WebMethod]
public List<int> List()
{
    yearList yl = new yearList();
    string connStr2 = ConfigurationManager.ConnectionStrings["taisangrent_sql"].ConnectionString;
    SqlConnection conn2 = new SqlConnection(connStr2);

    string strSQL2 = "select YEAR(getdate()) as year";
    SqlDataAdapter adapter2 = new SqlDataAdapter(strSQL2, conn2);

    DataSet ds2 = new DataSet();
    adapter2.Fill(ds2, "y_m");

    yl.year = int.Parse(ds2.Tables["y_m"].Rows[0]["year"].ToString());
    return yl.List();                    
}
Community
  • 1
  • 1
bbqwings
  • 85
  • 1
  • 4
  • 15
  • 1
    [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) – Soner Gönül Jul 18 '14 at 09:07

2 Answers2

4

try changing..

public List<int> list;

to

public List<int> list = new List<int>();

When you're calling list.Add(year);, the list object has now yet been newed up, so that's why you're getting the error.

I think it would be better to change the method name too...

public List<int> GetList() instead of public List<int> List();

EDIT: Good point by @Hassan

just to clarify, the DataAdapter.Fill() will Open/Close connections automatically.

One of the great features of ADO.NET is that the DataAdapter object's Fill and Update methods can open and close a connection automatically. The advantage of this is that it is not necessary to open the connection explicitly because the DataAdapter opens it for you at the moment right before it executes its SQL command against the database and then closes it right afterwards.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
  • 1
    please note `new List;` should be `new List();` – Hassan Jul 18 '14 at 09:09
  • Also please notice that sqlconnection is not opened and closed? do you think it is required here? I think it will good to recommend `using` statement. – Hassan Jul 18 '14 at 09:18
  • @HassanNisar, the `.Fill()` will implicitly call `open` & `close`, as far as I remember from ADO.NET days :) – Christian Phillips Jul 18 '14 at 09:21
  • @bbqwings, this seems to be a different question? However, you're only adding the year **2014** to the `year` variable, so that's what you should be getting back? What were you trying to return? – Christian Phillips Jul 18 '14 at 09:29
  • OK, I'm guessing you could have done something like `list.Add(year+i);`, but that's a guess since I don't know the requirement. – Christian Phillips Jul 18 '14 at 09:37
  • i found another question, if i add `static` into webmethod, how do i use webmethod at .cs? or can i do that? THANKS – bbqwings Jul 21 '14 at 02:34
0

When declaring as

public List<int> list;

your object is just creating, it wont assign the memory to it. So just decalre it like

public List<int> list = new List<int>();

And also try to give some other name to your function if its not a constructor as it already a class name.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
Shubhit304
  • 181
  • 1
  • 15