void Print_Click(object sender, EventArgs e)//打印按钮事件,将调用下一则cs的某个类的方法
    {
      DataTable dt = (new SqlQuery()).DtQuery(string.Format(@"
select
convert(char(10),mdatetime,102)        as 日期,
flag             as 状态,
cast([number] as char(7))+' '+cast([number]%7 as char(1))         as 票号,
'武夷山'         as 始发站,
arrival          as 目的站,
weight           as 计费重量,
[money]          as 票面航空运费,
oil              as 燃油加价,
beyond           as 超限费,
insurance        as 保险费,
price            as 公布单价,
[money]+oil      as 公布运费,
mperson          as 经办人,
gkind            as 备注
from waybill
where flag in (2, 10000) and mdatetime between '{0} 00:00:00' and '{1} 23:59:59' and airlineid={2} and mperson='{3}'
order by number", tb_begin.Text, tb_end.Text, cb_aircompany.SelectedValue, pub.str));
      ReportPrint rpt = new ReportPrint();
      rpt.PrinthyDaily(dt);
      GC.Collect();
    }//被调用的类和方法using Office2000;
using System.IO;
using System;
using System.Data;
using System.Windows.Forms;
namespace AF.Util
{
  public ReportPrint()
  {
    exl = new Excel2000();
  }
  public void PrinthyDaily(DataTable dthy)
  {
     int row = 8;
     exl.Caption = "货运销售日报打印";
    exl.Open(@"E:\VS\货运\货运销售日报.xls");
    exl.DisplayAlerts = false;
    if(dthy == null) return;
     foreach(DataRow d in dthy.Rows)
     {
          exl.SetCellText(row, "A", dr["票号"]);
       exl.SetCellText(row, "B", dr["始发站"]);
       exl.SetCellText(row, "C", dr["目的站"]);
       exl.SetCellText(row, "D", dr["计费重量"]);
       exl.SetCellText(row, "E", dr["票面航空运费"]);
       exl.SetCellText(row, "F", dr["燃油加价"]);
       exl.SetCellText(row, "G", dr["超限费"]);
       exl.SetCellText(row, "H", dr["保险费"]);
       exl.SetCellText(row, "I", dr["公布单价"]);
       exl.SetCellText(row, "J", dr["公布运费"]);
       exl.SetCellText(row, "K", dr["合计"]);
       exl.SetCellText(row, "L", dr["备注"]);
      row++;
      }   
   }
}
编译时没有出现问题。但运行就报错,如标题所示,请各位大虾指教,该怎么修改,

解决方案 »

  1.   

    调试下~~看看哪个对象为NULL了 
      

  2.   

    System.NullReferenceException: 未将对象引用设置到对象的实例。
       在 AF.Util.ReportPrint.PrinthyDaily(DataTable dthy)
       在 AF.Util.HYDaily.Print_Click(Object sender, EventArgs e)

       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    错误不实在数据库吧?