0

I have class SellStatement

public class SellStatement
{
    public long billNo
    public DateTime paymentDate;
    public List<string>  ProductName;
    public List<double> quantity;
    public List<double> ratePerQuantity;
}

When i am trying to access function GetSaleDetails

public Exception GetSaleDetails(List<SellStatement> lss1,string query)
    {
        try
        {
            for (int i = 0; i < lss1.ToArray().Length; i++)
            {
                query = "select * from [product details] where [bill no]=@billno";
                com = new SqlCeCommand(query, con);
                con.Open();
                com.Parameters.AddWithValue("@billno",lss1[i].billNo);
                sdr = com.ExecuteReader();
                while (sdr.Read())
                {
                    lss1[i].ProductName.Add(sdr.GetString(1));//Exception line
                    lss1[i].quantity.Add(sdr.GetDouble(2));
                    lss1[i].ratePerQuantity.Add(sdr.GetDouble(3));       
                }
            }
            con.Close();
            return null;
        }
        catch (Exception e)
        {
            con.Close();
            return e;
        }
    }

Null Reference Exception comes up atlss1[i].ProductName.Add(sdr.GetString(1));.I think error might be because of null value in at sdr.GetString(1) but i checked it has some value .My friend told me that you can't change Function argument value like this so i try to copy one list to other like this .

 List<SellStatement> lss1 = new List<SellStatement>() ;
            lss1.AddRange(lss);

But it doesn't help me out. I am not able to figure out what's wrong while adding element.

4
  • You need to learn how to handle exceptions... Commented May 2, 2013 at 10:51
  • please tell me What is wrong in this code . Commented May 2, 2013 at 10:52
  • First of all, you don't return an Exception... You throw them. See MSDN.Secondly, have you checked if your queries return valid values? You are not checking on anything. See MSDN. Commented May 2, 2013 at 10:54
  • bash.d this function is only for test and yes i checked my query values before executing this it return valid values. Commented May 2, 2013 at 10:56

1 Answer 1

4

If you showed us your complete SellStatement class in the question, then the reason is clear:
You never initialized ProductName, quantity and ratePerQuantity. They are null and that's exactly what the exception is telling you.

To fix it, change your class to this:

public class SellStatement
{
    public long billNo
    public DateTime paymentDate;
    public List<string> ProductName = new List<string>();
    public List<double> quantity = new List<double>();
    public List<double> ratePerQuantity = new List<double>();
}

Please note that this goes against the normal C# design guidelines that say you shouldn't have public fields. Consider redesigning your class like so:

public class SellStatement
{
    List<string> _productName = new List<string>();
    List<double> _quantity = new List<double>();
    List<double> _ratePerQuantity = new List<double>();

    public long billNo {get; set;}
    public DateTime paymentDate  {get; set;}
    public List<string> ProductName { get { return _productName; } }
    public List<double> quantity { get { return _quantity; } }
    public List<double> ratePerQuantity { get { return _ratePerQuantity; } }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is what my function needs Daniel you are true expert thank you very much and i am very sorry for such a foolish mistake .
@DGibbs: Why? lss1 is a list of SellStatement objects. This very well can be non-null and the place of his exception confirms this. The problem was that each of the SellStatement instances had members that were null. Have a close look at the line that throws the exception: lss1[i].ProductName.Add(sdr.GetString(1));: It calls Add on ProductName which is null. The NRE didn't occur because lss1 or lss1[i] were null.

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.