为什么在参数有值的情况下出现ObjectReference null错误?

时间:2018-07-12 11:31:28

标签: c# asp.net c#-4.0

这是我的代码,为什么会产生错误?

 sMailBody=MailstrBody.ToString();
                    StringBuilder sample = new StringBuilder();
                    sample = MailstrBody;

                                           string passno = dsMail1.Tables[0].Rows[0]["PASSNO"].ToString();
                    HttpResponse Response = HttpContext.Current.Response;

                    EmailContentDAL example = new EmailContentDAL();
                    example.Pdf(sample, Response, passno);      


     public void Pdf(StringBuilder sample, HttpResponse currentResponse, string passno)
    {

      StringReader sr = new StringReader(sample.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, 
        currentResponse.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
       currentResponse.Clear();
       currentResponse.ContentType = "application/pdf";
       currentResponse.AddHeader("Content-Disposition", "attachment; filename=GetPass_" + passno + ".pdf");

        currentResponse.Buffer = true;
        currentResponse.Cache.SetCacheability(HttpCacheability.NoCache);
        currentResponse.Write(pdfDoc);
        currentResponse.End();

}

上面的代码在htmlparser.Parse(sr)处产生空引用错误,但是在StringReader sr = new StringReader(sample.ToString())处,sample.toString()显示一个值。

0 个答案:

没有答案