473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

object reference not set to any instant exception

hi

i have a object to a user defined class which properly intialized from class when first form load, but after post back it loose reference and such error occured

[NullReferenceException: Object reference not set to an instance of an object.]
monthlyreport.GetReport() in c:\Time Entry System\WebTimeEntrySystem\monthlyreport.aspx.cs:10 0
monthlyreport.btn_show_report_Click(Object sender, EventArgs e) in c:\Time Entry System\WebTimeEntrySystem\monthlyreport.aspx.cs:94
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

My c# code is
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. using TimeEntrySystem;
  13. using TimeEntrySystem.BLL;
  14.  
  15. public partial class dailyreport : System.Web.UI.Page
  16. {
  17.     private DataTable objDataTable;
  18.     private Reports objReport;
  19.  
  20.     protected void btn_show_report_Click(object sender, EventArgs e)
  21.     {
  22.         DateTime startingDate = Convert.ToDateTime(tb_daily_report_date.Text);
  23.  
  24.         #region
  25.         btn_daily_report_show.Enabled = false;
  26.         DailyReport(startingDate);
  27.         btn_daily_report_show.Enabled = true;
  28.         #endregion
  29.     }
  30.  
  31.     public void DailyReport(DateTime startingDate)
  32.     {
  33.         objDataTable =  objReport.ReturnDailyReport(startingDate);
  34.         if (objDataTable == null)
  35.         {
  36.             lb_daily_report_error.Text = "There is some error in record reports.";
  37.         }
  38.         if (objDataTable.Rows.Count == 0)
  39.         {
  40.             lb_daily_report_error.Text = "No Record found.";
  41.             objDataTable.Clear();
  42.             return;
  43.         }
  44.     }
  45.  
  46.     public DataTable ObjDataTable
  47.     {
  48.         get { return objDataTable; }        
  49.     }
  50.     protected void Page_Load(object sender, EventArgs e)
  51.     {        
  52.         if (!IsPostBack)
  53.         {
  54.             objDataTable = new DataTable("DailyReports");
  55.             objReport = new Reports();
  56.  
  57.             DateTime startingDate = DateTime.Now.Date;
  58.             tb_daily_report_date.Text = startingDate.Date.ToShortDateString();            
  59.             DailyReport(startingDate);
  60.         }
  61.         lb_report_duration1.Text = "Daily Report";
  62.         lb_report_duration2.Text = tb_daily_report_date.Text;
  63.  
  64.         vld_range_daily_report_tb_start_date.MinimumValue = objReport.GetMinDate().ToShortDateString();
  65.         vld_range_daily_report_tb_start_date.MaximumValue = DateTime.Now.ToShortDateString();
  66.  
  67.     }
  68.     protected void start_img_Click(object sender, ImageClickEventArgs e)
  69.     {
  70.         tb_daily_report_date.Text = cld_tb_daily_report_date.SelectedDate.Value.ToString();
  71.     }
  72. }
after post objReport and objTable loose its reference from their classes

so please any body help me about that problem thanks
Sep 5 '08 #1
4 1526
Plater
7,872 Expert 4TB
The error says it occurs on the monthlyreport page, yet you have shown code for the dailyreport page. Do you have a monthly report page you could show us code for?
Sep 5 '08 #2
The error says it occurs on the monthlyreport page, yet you have shown code for the dailyreport page. Do you have a monthly report page you could show us code for?
sorry man my fault i replace the code of daily report ....
but error is same in daily report page and monthly report page this is monthly report page
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using TimeEntrySystem.BLL;
  12. using System.Collections.Generic;
  13.  
  14. public partial class monthlyreport : System.Web.UI.Page
  15. {
  16.     private Reports objReport;
  17.     private IList<Employee> lstEmployee;
  18.     private Employee objEmployee;
  19.     private IList<int> lstYear;
  20.     private IList<string> lstMonth;
  21.     private DataTable objDataTable;
  22.  
  23.     protected void Page_Load(object sender, EventArgs e)
  24.     {
  25.  
  26.         if (Session["user_id"] == null)
  27.         {
  28.             Response.Redirect("login.aspx");
  29.         }        
  30.  
  31.         if (!IsPostBack)
  32.         {
  33.             objReport = new Reports();
  34.             lstEmployee = new List<Employee>();
  35.             objEmployee = new Employee();
  36.             lstYear = new List<int>();
  37.             lstMonth = new List<string>();
  38.             objDataTable = new DataTable();
  39.  
  40.             lstEmployee = objReport.GetAllEmployeesRegistered();
  41.             lstYear = objReport.GetAllYearsRegistered();
  42.  
  43.             objEmployee.Employee_Name = "All";
  44.             objEmployee.UserId = "All";
  45.             lstEmployee.Insert(0, objEmployee);
  46.  
  47.             foreach (Employee item in lstEmployee)
  48.             {
  49.                 dpl_monthly_report_user_id.Items.Add(item.Employee_Name);
  50.             }
  51.             dpl_monthly_report_user_id.SelectedIndex = 0;
  52.  
  53.             if (lstYear.Count > 0)
  54.             {
  55.                 lstMonth = objReport.GetMonthsRegistered(lstYear[lstYear.Count - 1]);
  56.             }
  57.             else
  58.             {
  59.                 return;
  60.             }
  61.  
  62.             foreach (int item in lstYear)
  63.             {
  64.                 dpl_monthly_report_year.Items.Add(item.ToString());
  65.             }
  66.             dpl_monthly_report_year.SelectedIndex = lstYear.Count - 1;
  67.  
  68.             foreach (string item in lstMonth)
  69.             {
  70.                 dpl_monthly_report_month.Items.Add(item);
  71.             }
  72.  
  73.             if (lstMonth.Count > 0)
  74.             {
  75.                 dpl_monthly_report_month.SelectedIndex = lstMonth.Count-1;
  76.             }            
  77.             GetReport();
  78.         }
  79.     }
  80.     protected void dpl_year_SelectedIndexChanged(object sender, EventArgs e)
  81.     {
  82.         lstMonth = objReport.GetMonthsRegistered(Convert.ToInt32(dpl_monthly_report_year.SelectedValue));
  83.         dpl_monthly_report_month.Items.Clear();
  84.         foreach(string item in lstMonth)
  85.         {
  86.             dpl_monthly_report_month.Items.Add(item);
  87.         }
  88.         dpl_monthly_report_month.SelectedIndex = lstMonth.Count-1;
  89.         GetReport();       
  90.     }
  91.     protected void btn_show_report_Click(object sender, EventArgs e)
  92.     {        
  93.         btn_monthly_report_show_report.Enabled = false;
  94.         GetReport();
  95.         btn_monthly_report_show_report.Enabled = true;        
  96.     }
  97.  
  98.     public void GetReport()
  99.     {
  100.         objDataTable.Clear();
  101.         objDataTable = objReport.ReturnMothlyReportDataTable(Convert.ToInt32(dpl_monthly_report_year.SelectedValue), dpl_monthly_report_month.SelectedValue.ToString(), lstEmployee[dpl_monthly_report_user_id.SelectedIndex].UserId);
  102.  
  103.         if (objDataTable == null)
  104.         {
  105.             lb_monthly_report_error.Text = "There is some error in reports.";
  106.         }
  107.         if (objDataTable.Rows.Count == 0)
  108.         {
  109.             lb_monthly_report_error.Text = "No Record found.";
  110.         }
  111.     }
  112.  
  113.     public DataTable ObjDataTable
  114.     {
  115.         get { return objDataTable; }
  116.     }    
  117. }
Sep 5 '08 #3
Curtis Rutland
3,256 Expert 2GB
shahidrasul, please use code tags when you post [code] tags when you post code. It makes code much easier for us to read, and if we can't read it, we can't help. You can click the # button in the text editor to add the tags for you.

MODERATOR
Sep 5 '08 #4
Plater
7,872 Expert 4TB
Well I see this code:
Expand|Select|Wrap|Line Numbers
  1. if (objDataTable == null)
  2.         {
  3.             lb_monthly_report_error.Text = "There is some error in reports.";
  4.         }
  5.         if (objDataTable.Rows.Count == 0)
  6.         {
  7.             lb_monthly_report_error.Text = "No Record found.";
  8.         }
  9.  
Now you are checking for null, which is good, but if it IS null, the second check is still being attempted, and could result in the error you are seeing.

Also, anthing in this line:
objDataTable = objReport.ReturnMothlyReportDataTable(Convert.ToIn t32(dpl_monthly_report_year.SelectedValue), dpl_monthly_report_month.SelectedValue.ToString(), lstEmployee[dpl_monthly_report_user_id.SelectedIndex].UserId);

could also cause the error if one of them were null
Sep 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
3
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following...
3
by: nemo | last post by:
Hi, My application works fine on the localhost but spits this error as soon as I put it on the server. I know this error occurs when an object has not been instantiated prior to a reference, but...
4
by: Arapi | last post by:
I am having a very strange problem with my asp.net application. I am getting error: 'Object reference not set to an instance of an object' when I run my app from my hosting company. I have...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
8
by: Vik | last post by:
I need to make some calculations, e.g. addition, on the objects x and y which are numbers (of the same or different types) and may be DBNulls. In VB, the following code is correct: Public Shared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.