解决NullReference异常 [英] Solve NullReference exception

查看:309
本文介绍了解决NullReference异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private ReportDocument _reportDocument; 
public CrystalReportPrintPreviewForm(ReportDocument reportDocument)
{
InitializeComponent();

_reportDocument = reportDocument;
}
private void CrystalReportPrintPreviewForm_Load(object sender,EventArgs e)
{
if(_reportDocument!= null)
crystalReportViewer1.ReportSource = _reportDocument;
}

而且我还使用以下代码将ReportDocument发送到此表单并显示它:

  ReportDocument reportDocument = new ReportDocument(); 
reportDocument.Load(Application.StartupPath +\\Reports\\\WorkGroupReport.rpt);
kargarBandarDataset.WorkGroup.DefaultView.RowFilter = workGroupBindingSource.Filter;
reportDocument.SetDataSource(kargarBandarDataset.WorkGroup.DefaultView.ToTable());
reportDocument.SetParameterValue(CurrentDate,shamsi.ShamsiDate());

CrystalReportPrintPreviewForm crystalReportPrintPreview = new CrystalReportPrintPreviewForm(reportDocument);
crystalReportPrintPreview.ShowDialog();

有时我会在代码的以下行中得到NullReferenceException错误消息:

  crystalReportPrintPreview.ShowDialog(); 

我如何解决这个问题?

解决方案

没有确切的答案如何处理NullReferenceException。确保您启用了VS(Debug-> Exception mark Common Language Runtime)中抛出的期望值cathching,找到抛出的地方,并尝试不理解原因。



另外,尝试从.Exception的.Message属性中获取更多的信息。



通常由引用到方法或类的构造函数的一些空参数引起的NullReference。尝试调试,看看你传递给对象的东西会抛出异常;


I use crystal report to implement reporting in my c# windows application.I create a form to show print preview of Report.I use following code to show preview:

private ReportDocument _reportDocument;
public CrystalReportPrintPreviewForm(ReportDocument reportDocument)
{
    InitializeComponent();

    _reportDocument = reportDocument;
}
private void CrystalReportPrintPreviewForm_Load(object sender, EventArgs e)
{
    if(_reportDocument!=null)
    crystalReportViewer1.ReportSource = _reportDocument;
}

And also i use following code to send 'ReportDocument' to this form and show it:

ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(Application.StartupPath + "\\Reports\\WorkGroupReport.rpt");
kargarBandarDataset.WorkGroup.DefaultView.RowFilter = workGroupBindingSource.Filter;
reportDocument.SetDataSource(kargarBandarDataset.WorkGroup.DefaultView.ToTable());
reportDocument.SetParameterValue("CurrentDate",shamsi.ShamsiDate());

CrystalReportPrintPreviewForm crystalReportPrintPreview = new CrystalReportPrintPreviewForm(reportDocument);
crystalReportPrintPreview.ShowDialog();

Sometimes I get NullReferenceException error message in the following line of Code:

crystalReportPrintPreview.ShowDialog();

How Can i solve this problem?

解决方案

There is no exact answer of how to handle NullReferenceException. Make sure that you enabled "cathching" of thrown expections in VS (Debug->Exception mark Common Language Runtime) find the place where expection if thrown and try to undestand the reasons.

Also, try to get as much more information from .Message property of Exception.

Typically NullReference caused by some null arguments submitting to method or constructor of class. Try to debug and see what you passing to object that throws an exception;

这篇关于解决NullReference异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆