[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

System.NullReferenceException was unhandled by user code

Posted on 2008-11-10
10
Medium Priority
?
2,491 Views
Last Modified: 2012-05-05
Hi,

I am working on ASP.net2.0 and C#

I have created a delegate method
public event CommandEventHandler SaveReportImageButtonClicked;


But when I write

SaveReportImageButtonClicked(this, new CommandEventArgs("ExportImageTo", strExportTo));

It throws an error


System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_luvuxx3t"
  StackTrace:
       at Reports_ReportsMaster.imgExportReportToExcel_Click(Object sender, ImageClickEventArgs e) in d:\Dinesh\Projects\IssueManager_Latest\IssueManager_Latest\IssueManager\Reports\ReportsMaster.master.cs:line 111
       at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
       at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


But My delegate public event CommandEventHandler MonthsButtonClicked;
works fine

Can anyone help me in this.

Note: In CommandEventHandler MonthsButtonClicked, button is clicked

and in public event CommandEventHandler SaveReportImageButtonClicked;

is a Image button clicked

Below is my code

Please guide
public event CommandEventHandler MonthsButtonClicked;
public event CommandEventHandler SaveReportImageButtonClicked;
 
 
protected void btnViewReport_Click(object sender, EventArgs e)
{        
	if (txtMonths.Text != string.Empty && Convert.ToInt32(txtMonths.Text) > 0)
	    MonthsButtonClicked(this, new CommandEventArgs(ddlProjects.SelectedValue, txtMonths.Text));
}
 
 
protected void imgExportReportToPDF_Click(object sender, ImageClickEventArgs e)
{
    string strExportTo = "PDF";
    SaveReportImageButtonClicked(this, new CommandEventArgs("ExportImageTo", strExportTo));
}
 
 
 
IN ASPX MASTER PAGE IT is written
 
<td style="text-align: left" id="tdCommonControl" runat="server">
<asp:Panel ID="pnlCommonControl" runat="server">
 
    <asp:Label ID="lblMonths" runat="server" Text="Last Months: " Style="font-weight: 700"></asp:Label>&nbsp;
    <asp:TextBox
	ID="txtMonths" runat="server" Width="50px"></asp:TextBox>&nbsp;<asp:Button ID="btnViewReport"
	    runat="server" Text="View Report" CssClass="leftMenuHeader" OnClick="btnViewReport_Click" />
</asp:Panel>
 
</td>
<td>
<asp:Label ID="Label1" runat="server" Style="font-weight: 700" Text="Export Report: "></asp:Label><br />
<asp:ImageButton ID="imgExportReportToExcel" runat="server" CommandArgument="ExportToExcel"
    CommandName="ExportToExcel" ImageUrl="~/images/XLS_true.gif" OnClick="imgExportReportToExcel_Click" />&nbsp;
    
    <asp:ImageButton
	ID="imgExportReportToPDF" runat="server" CommandArgument="ExportToPDF" CommandName="ExportToPDF"
	ImageUrl="~/images/PDF_true.gif" OnClick="imgExportReportToPDF_Click" />
	
 
</td>

Open in new window

0
Comment
Question by:tia_kamakshi
  • 5
  • 4
10 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 22919905
Hi 
you must  initialize it so as to knows that such a delegate exists
in you 
Me.your_object_here.Click += new System.EventHandler(Me.SaveReportImageButtonClicked);
0
 

Author Comment

by:tia_kamakshi
ID: 22919980

Hi,

Thanks for your reply,

This bit I have already written in ASPX file

and the above code is written in Master File



It works good with

Master.ProjectIdChanged += new CommandEventHandler(Master_ProjectIdChanged);

But now with
Master.SaveReportImageButtonClicked += new CommandEventHandler(Master_ExportReport);  

May ne the difference is that

SaveReportImageButtonClicked is using Image button and
Master_ProjectIdChanged is using ASPX button

Please guide
Below is my code in ASPX file
 
 
protected void Page_Init(object sender, EventArgs e)
{        
	Master.ProjectIdChanged += new CommandEventHandler(Master_ProjectIdChanged);
	Master.SaveReportImageButtonClicked += new CommandEventHandler(Master_ExportReport);  
}
 
protected void Master_ProjectIdChanged(object sender, CommandEventArgs e)
{
	int intProjectId = 0;
	string strProjectName = e.CommandName;
 
	intProjectId = Convert.ToInt32(e.CommandArgument.ToString());
	LoadReportsData(intProjectId);
}
 
 
protected void Master_ExportReport(object sender, CommandEventArgs e)
{
	string strExportReportTo = e.CommandName;
	SaveReports(strExportReportTo);
}

Open in new window

0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22920123
Hi there,

The "e" argument for the ImageButton should be of type ImageClickEventArgs:

SaveReportImageButtonClicked(this, new ImageClickEventArgs(xPosition, yPosition));

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tia_kamakshi
ID: 22920158
But in command argument I wish to pass string

and in aspx file I will take this string from command argument

and in

new ImageClickEventArgs(xPosition, yPosition))

xPosition and yPosition are the int values, where user has clicked on the image

Which does not meet my requirement

Please look into this
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22920182
Then you need to change this:

OnClick="imgExportReportToExcel_Click"

with this:

OnCommand="imgExportReportToExcel_Command"

And the same with the other ImageButton.
0
 

Author Comment

by:tia_kamakshi
ID: 22920312
Now, at below line I get an exception

SaveReportImageButtonClicked(this, new CommandEventArgs(imgExportReportToExcel.CommandName, imgExportReportToExcel.CommandArgument));

I get an exception

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_aa3nwd0_"
  StackTrace:
       at Reports_ReportsMaster.imgExportReportToExcel_Command(Object sender, CommandEventArgs e) in d:\Dinesh\Projects\IssueManager_Latest\IssueManager_Latest\IssueManager\Reports\ReportsMaster.master.cs:line 137
       at System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       
       
       
AND WHEN I WRITE



protected void imgExportReportToExcel_Command(object sender, CommandEventArgs e)
{
        SaveReportImageButtonClicked(this, new CommandEventArgs("Excel"));
}


Then I get an Exception      
       
Argument '1': cannot convert from 'string' to 'System.Web.UI.WebControls.CommandEventArgs'


How can I pass the string here

Please guide

Many Thanks
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22920329
The code below is almost fine, you just need to set the second parameter:

protected void imgExportReportToExcel_Command(object sender, CommandEventArgs e)
{
        SaveReportImageButtonClicked(this, new CommandEventArgs("Excel",String.Empty ));
}

Open in new window

0
 

Author Comment

by:tia_kamakshi
ID: 22920362
protected void imgExportReportToExcel_Command(object sender, CommandEventArgs e)
{
      SaveReportImageButtonClicked(this, new CommandEventArgs("Excel", String.Empty));
}


Still At line
SaveReportImageButtonClicked(this, new CommandEventArgs("Excel", String.Empty));

I get an error


System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_abrdv_io"
  StackTrace:
       at Reports_ReportsMaster.imgExportReportToExcel_Command(Object sender, CommandEventArgs e) in d:\Dinesh\Projects\IssueManager_Latest\IssueManager_Latest\IssueManager\Reports\ReportsMaster.master.cs:line 138
       at System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


(:
0
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 2000 total points
ID: 22924361
Mmm.....

Is the Master object not null here?:

protected void Page_Init(object sender, EventArgs e)
{        
        Master.ProjectIdChanged += new CommandEventHandler(Master_ProjectIdChanged);
        Master.SaveReportImageButtonClicked += new CommandEventHandler(Master_ExportReport);  
}

Can you please check that?

0
 

Author Closing Comment

by:tia_kamakshi
ID: 31514956
Thanks you are right.

It works great now.

Thanks again
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question