Click here to Skip to main content
15,901,680 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i have an error.The error is Object reference not set to an instance of an object.
the follwing line shows the error
Admin_WebSite_EditControl.LoadHeaders() in c:\inetpub\vhosts\schoolwebsites.com.au\httpdocs\Admin\WebSite\Edit.ascx.cs:line 619
pls help me?
the code is given below


C#
private void LoadHeaders()
   {




       for (int index = 1; index <= NO_OF_HEADERS_TO_DISPLAY; index++)
       {

           var uiUploadHeader = FindControl(string.Format("uiUploadHeaderImage{0}", index)) as Admin_Controls_Upload;
           var uiTxtHeaderText = FindControl(string.Format("uiTxtHeaderText{0}", index)) as TextBox;

           var uiHidHeaderID = FindControl(string.Format("uiHidHeaderID{0}", index)) as HiddenField;

           WebSiteHeader header;
           bool isNewHeader = false,
                   headerUploaded = false;
           int headerID;

           int.TryParse(uiHidHeaderID.Value, out headerID);
           if (headerID > 0)
           {
               header = Webcoda.Cms.DAL.Context.Instance.WebSiteHeaders.Where(o => o.ID == headerID).SingleOrDefault();
           }
           else
           {
               isNewHeader = true;
               header = new WebSiteHeader();


           }

           if (index > 1)
           {
               // Delete header
               if (uiUploadHeader.DeleteCurrentFile && !isNewHeader)
               {
                   uiUploadHeader.DeleteImage(string.Format("{0}{1}", this.DataSource.HeaderImagesPath, header.HeaderImageSrc));
                   Webcoda.Cms.DAL.Context.Instance.WebSiteHeaders.DeleteOnSubmit(header);
                   return;
               }

               //  Update / Create  header



               string headerImageName;
               uiUploadHeader.Fill(this.DataSource.HeaderImagesPath, string.Format("{0}_{1}", this.HEADER_IMAGE_DEFAULT, index), true, true, out headerImageName);
               if (!string.IsNullOrEmpty(headerImageName))
               {
                   headerUploaded = true;
                   header.HeaderImageSrc = headerImageName;
               }
               else
               {

               }

           }
           else
           {
               headerUploaded = true;
               header.HeaderImageSrc = this.DataSource.WebSiteStyles[0].HeaderImageSrc;
           }
           header.HeaderImageText = uiTxtHeaderText.Text;

           if (isNewHeader && headerUploaded)
           {
               this.DataSource.WebSiteHeaders.Add(header);
           }

       }
   }





619 code line given below

C#
string headerImageName;
               uiUploadHeader.Fill(this.DataSource.HeaderImagesPath, string.Format("{0}_{1}", this.HEADER_IMAGE_DEFAULT, index), true, true, out headerImageName);
               if (!string.IsNullOrEmpty(headerImageName))
               {
                   headerUploaded = true;
                   header.HeaderImageSrc = headerImageName; //619th line
               }
               else
               {

               }
Posted
Updated 25-Jun-15 22:39pm
v3
Comments
What is in line 619?
F-ES Sitecore 26-Jun-15 3:15am    
Even better than telling us what line this happens on so there is the slightest chance of someone helping you, learn to debug your code so that you can fix these issues yourself

http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn
Thanks7872 26-Jun-15 3:18am    
Nobody here knows what line 619 contains. No need to post whole code here. At lin 619, there should be one null object which is causing an issue. Try to make it such that its not null at that point.
Member 10199401 26-Jun-15 3:29am    
Can you tell me on which line you getting this error. Its hard to know in whole code without knowing the working of this ;)

header = Webcoda.Cms.DAL.Context.Instance.WebSiteHeaders.Where(o => o.ID == headerID).SingleOrDefault();

Just try to change below line

header = Webcoda.Cms.DAL.Context.Instance.WebSiteHeaders.Where(o => o.ID == headerID).SingleOrDefault();

into
header = Webcoda.Cms.DAL.Context.Instance.WebSiteHeaders.FirstOrDefault(o => o.ID == headerID);

and check null for 'header' before using this.

Try to debug this and check each oject value that you are using. Some oject is null and you trying to access that object property in other block
Athul MS 26-Jun-15 7:13am    
not working

Use Ctrl+G in Visual Studio and type in 619 to go to the specific line number on Edit.ascx.cs page.

Now check the code and make sure you handle all variables or class instances for any null values.
 
Share this answer
 
Use the debugger.

Start by putting a breakpoint on "line 619" of your file, and run your application in the debugger. when it reaches the line, it will pause and let you look at what is happening.
Start on the left of the line, and check exactly what each part of the commend contains. You are looking for which bit of what you wrote contains a null value.
In this case:
C#
Admin_WebSite_EditControl.LoadHeaders();
It's most likely that Admin_WebSite_EditControl has no value set, for some reason (but without the actual code, your data, and running it we can't tell)
When you know which value is null, you can start looking back through your code to find out why it's not set. But we can't do that for you!
 
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