Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
its giving me error at runtime and i cant understand error please help me ?
please advice


C#
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    {
        int month;
        month= DropDownList4.SelectedIndex  ;
        int year=Convert.ToInt32 (DropDownList3.Text)  ;
        SignUp obj=new SignUp();
        int p = 31;
        DropDownList5.Items.Add("0");
        switch (month)
        {
            case 0:
                obj.days( ref p);
                break;




    }

    public  void days(ref int day)
    {

        for (int i = 1; i <=day; i++)
        {

            DropDownList5.Items.Add(i.ToString() );

        }

    }
}

Error :
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 95: {
Line 96:
Line 97: DropDownList5.Items.Add(i.ToString() );
Line 98:
Line 99: }
Posted
Updated 25-Nov-12 20:59pm
v3

Hi Chandrakant,

Write your code as below.

C#
protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{
   int month;
   month= DropDownList4.SelectedIndex  ;
   int year=Convert.ToInt32 (DropDownList3.SelectedItem.Text)  ;
   SignUp obj=new SignUp();
   int p = 31;
   ListItem li = new ListItem("0","0");
   DropDownList5.Items.Add(li);
   switch (month)
   {
      case 0:
           obj.days( ref p);
           break;
    }
 
public  void days(ref int day)
{
   for (int i = 1; i <=day; i++)
   { 
       ListItem li = new ListItem(i.ToString(),i.ToString());
       DropDownList5.Items.Add(li); 
   } 
}
 
Share this answer
 
v2
The line that is throwing the error:
C#
DropDownList5.Items.Add(i.ToString() );
Has only two parts that use a method:
C#
i.ToString()
And
C#
DropDownList5.Items.Add(...);
The first is using an integer, which is not a nullable type, so it can't be that.
The second is using a collection of an object - if the object exists, then it will always have an Items Collection (because it is constructed as part of the DropDownList constructor) so the only nullable object is DropDownList5

Look at your other code - you need to work out how you get into the days method withput a value in DropDownList5 - where do you set it's value? What do you set it to? What are you doing when you call days?

Use the debugger - examine your stack trace, and look at the variables involved.
We can't solve this - you have the information we don't!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900