变量“doc”在 if 语句之外的方法中的其他任何地方都无法访问,因此 if doc==null 失败,因为“doc”的范围仅在定义它的那些 if 语句中......如何做我处理这个问题?添加 public 只会导致更多错误..
protected void Page_Load(object sender, EventArgs e)
{
try
{
string id, type, UniqueColID;
string FilePath = Server.MapPath("~").ToString();
type = Request.QueryString["type"];
if (type.Equals("template"))
{
MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
template.DrawToWeb();
}
else
{
id = Request.QueryString["id"];
UniqueColID = DBFunctions.DBFunctions.testExist(id);
if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
{
MergeDocument doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
}
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
{
MergeDocument doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
}
DBFunctions.DBFunctions.FlagDriverPrintOnly = false;
if (doc == null)
doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
doc.DrawToWeb();
}
}
catch(Exception err)
{
MessageBox("Creating PDF file was not successful. " + err.ToString());
}
我尝试在更高级别声明它,但我仍然得到同样的错误:
**Use of unassigned local variable 'doc' -->>>at: if (doc==nul)**
做完 MergeDocument=null; 在更高级别我收到一个新错误:
System.NullReferenceException: Object reference not set to an instance of an object. at GeneratePDF.Page_Load(Object sender, EventArgs e)
这个错误指向"if (type.Equals("template"))"