Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Server Error in '/WebSite2' Application.
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 349:            ddlYear.Items.Add(i.ToString());
Line 350:        }
<big></big>Line 351:        ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true;  //set current year as selected
Line 352:
Line 353:        //Fill Months with current month selected


Source File: c:\Users\Ashwini\Documents\Visual Studio 2010\WebSites\WebSite2\DesignPage.aspx.cs    Line: 351

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   DesignPage.Clear_Controls() in c:\Users\Ashwini\Documents\Visual Studio 2010\WebSites\WebSite2\DesignPage.aspx.cs:351
   DesignPage.btnReset_Click(Object sender, EventArgs e) in c:\Users\Ashwini\Documents\Visual Studio 2010\WebSites\WebSite2\DesignPage.aspx.cs:332
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552874
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18055
Posted
Updated 23-Jan-14 21:57pm
v5
Comments
Karthik_Mahalingam 24-Jan-14 3:28am    
post your code..

i think u refer as a null value,try to use break point and check ,ddlYear.Items have current year,in dropdown list have current year ?

ddlYear.Items.Add(i.ToString()); here need to check year is added in ddlyear ?
 
Share this answer
 
Suppose assigned year is not found in the dropdown items, then it will give a error. so better assign the value to SelectedValue properly.

or

Suppose you don't have value property and want to select the value based on the Text property then please go for the Extended method for Dropdown

C#
public static void SelectText(this DropDownList ddlExt, string text)
       {
           try
           {
               ListItem lstItem = ddlExt.Items.FindByText(text);

               if (lstItem != null)
               {
                   if (ddlExt.SelectedIndex >= 0)
                       ddlExt.Items[ddlExt.SelectedIndex].Selected = false;
                   lstItem.Selected = true;
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }


And you can assign the value as

ddlFrom.SelectText("2014")
 
Share this answer
 
Simple: There is no entry in your drop down list that has a value exactly matching the current year converted to a string.

Why not? Don't know - we don;t have anywhere near enough context to tell you.
So start with either the debugger (put a breakpoint on the first instruction in the method and step through) or by logging the values you are adding to a file of some form and look at what is going on. At the moment, you don;t have enough information to decide what the problem might be exactly, so you have to gather more info first.
 
Share this answer
 

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



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