这是什么错???

acxcod 2003-08-02 11:10:40
以下代码(在打印机上打印),出错,不知该怎样???
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Printing;
using System.IO;



namespace WebApplication4
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.ComponentModel.Container components;
protected System.Web.UI.WebControls.Button Button1;
protected Font printFont;
protected StreamReader streamToPrint;

private void Page_Init(object sedner, EventArgs e)
{
InitializeComponent();
}
private void Page_Load(object sedner, EventArgs e)
{
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Button1.Click += new stem.EventHandlerthis.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void pd_printPage(object sender,PrintPageEventArgs ev)
{
float lpp =0 ;
float ypos =0;
int counter =0;
float leftMar = ev.MarginBounds.Left;
float topMar = ev.MarginBounds.Top;
string textline = null;
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
while (counter < lpp && ((textline=streamToPrint.ReadLine())!= null))
{
ypos = topMar+(counter * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawStringtextline,printFont,Brushes.Black,leftMar,y pos,new StringFormat());
counter++;
}
}

private void Button1_Click(object sender, System.EventArgs e)
{
try
{
StreamReader streamToPrint=new StreamReader(@"c:\abc.txt");
try
{
Font PrintFont = new Font("Arial",12);
PrintDocument pd = new PrintDocument();
pd.PrintPage+=new PrintPageEventHandler(this.pd_printPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
Label1.Text ="Error printing file:" + ex.ToString();
}
}

}
}


出错提示(也就是Label1.Text的内容)为:
Error printing file:System.NullReferenceException: 未将对象引用设置到对象的实例。 at WebApplication4.WebForm1.pd_printPage(Object sender, PrintPageEventArgs ev) in c:\inetpub\wwwroot\webapplication4\webform1.aspx.cs:line 68 at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at WebApplication4.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication4\webform1.aspx.cs:line 87

我刚学c#,水平有限,望高手指点!
...全文
94 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
acxcod 2003-08-03
  • 打赏
  • 举报
回复
对,就象 shixueli(渴望成功)讲的那样, 在这个函数中实例化对象,但又在那个函数中
使用(没实例化), 这个例子中有多处这样的错误,这个作者或许在出书时一定还在怀念着c++
年代的全局变量.
多谢 shixueli(渴望成功) 这样热心的高手.
shixueli 2003-08-03
  • 打赏
  • 举报
回复
很奇怪为什么你定义了
protected StreamReader streamToPrint;

在Button1_Click还要重新定义
StreamReader streamToPrint=new StreamReader(@"c:\abc.txt");

而pd_printPage()中用到的是第一个定义的那个,这是没有用new初始化的,当然出错了
acxcod 2003-08-02
  • 打赏
  • 举报
回复
应该是这一句错,但错在哪呢?
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
acxcod 2003-08-02
  • 打赏
  • 举报
回复
真没人知道吗?
wayneliu 2003-08-02
  • 打赏
  • 举报
回复
代码太长了,不过一般像这样的错误都是小错误!

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧