【发布时间】:2020-09-04 14:42:19
【问题描述】:
文件上传在本地工作正常,但在服务器中部署时出错。 错误信息是
“/”应用程序中的服务器错误。 你调用的对象是空的。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
来源错误:
在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
堆栈跟踪:
[NullReferenceException:对象引用未设置为对象的实例。] Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingConfigurationView.GetExceptionPolicyData(字符串策略名称)+186 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.GetConfiguration(字符串 id,IConfigurationSource 配置源)+102 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.CreateObject(IBuilderContext 上下文,字符串名称,IConfigurationSource 配置源,ConfigurationReflectionCache 反射缓存)+94 Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.PreBuildUp(IBuilderContext 上下文)+325 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext 上下文) +138
[BuildFailedException:当前构建操作(构建密钥 Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,UIExceptionPolicy])失败:对象引用未设置为对象的实例。 (策略类型ConfiguredObjectStrategy,索引2)] Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext 上下文)+498 Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator 定位器,ILifetimeContainer 生命周期,IPolicyList 策略,IStrategyChain 策略,对象 buildKey,现有对象)+65 Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp(IReadWriteLocator 定位器,ILifetimeContainerlifetimeContainer,字符串 id,IConfigurationSource 配置源)+729 Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.Create(字符串名称)+187 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory) +90
[ExceptionHandlingException:当前构建操作(构建键 Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,UIExceptionPolicy])失败:对象引用未设置为对象的实例。 (策略类型ConfiguredObjectStrategy,索引2)] Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(异常异常,字符串策略名称,ExceptionPolicyFactory 工厂)+494 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(异常 exceptionToHandle,字符串 policyName,ExceptionPolicyFactory policyFactory)+75 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(异常异常ToHandle,字符串策略名称)+255 E:\PROJECT\DEVELOPMENT\WEBSITE\FOCUS-TUTORIAL\Focustutorial\MFBMS.Web\Focuspages\FileUploader.aspx.cs:185 中的 MFBMS.Web.Focuspages.Videouploader.btnUploadDocument(Object sender, EventArgs e) System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11594496 System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 eventArgument)+274 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1964
)
这是我尝试的代码
protected void btnUploadDocument(object sender, EventArgs e)
{
try
{
string PathToSave = "";
string FolderLocation = "";
string FileName = "";
string PathToDB = "";
string FileType = "";
if (!DocumentFileUpload.HasFile)
{
DocumentFileUpload.Focus();
lblUploadMsg.Visible = true;
lblUploadMsg.Text = "Select File Name.";
lblUploadMsg.Focus();
lblUploadMsg.CssClass = "FailedAlert";
return;
}
else
{
FileUploaderBO objBO = new FileUploaderBO();
FileUploaderData objdata = new FileUploaderData();
//****** FOR SAVING IMAGE TO DATABASE******//
//int length = DocumentFileUpload.PostedFile.ContentLength;
//byte[] imgbyte = new byte[length];
//HttpPostedFile img = DocumentFileUpload.PostedFile;
//img.InputStream.Read(imgbyte, 0, length);
//objdata.ByteImage = imgbyte;
//****** End FOR SAVING IMAGE TO DATABASE******//
FileType = ddlFileType.SelectedItem.Text;
FolderLocation = Server.MapPath(@"~/FileFolder/" + FileType + "/");
if (!Directory.Exists(FolderLocation))
{
Directory.CreateDirectory(FolderLocation);
}
PathToSave = Server.MapPath(@"~/FileFolder/" + FileType + "/");
FileName = DocumentFileUpload.FileName;
PathToDB = "~/FileFolder/" + FileType + "/" + FileName;
PathToDB = "~/FileFolder/" + FileType + "/" + FileName;
string fileCheck = PathToSave + FileName;
string tempFileToCheck = "";
if (System.IO.File.Exists(fileCheck))
{
int counter = 2;
while (System.IO.File.Exists(fileCheck))
{
tempFileToCheck = counter.ToString() + FileName;
fileCheck = PathToSave + tempFileToCheck;
counter++;
FileName = tempFileToCheck;
lblUploadMsg.Text = "A file with the same name is already exist." + "<br/> your file was saved as" + FileName;
lblUploadMsg.Focus();
lblUploadMsg.CssClass = "FailedAlert";
}
}
else
{
PathToSave += FileName;
DocumentFileUpload.SaveAs(PathToSave);
}
objdata.FileTypeID = Convert.ToInt32(ddlFileType.SelectedValue == "" ? "0" : ddlFileType.SelectedValue);
objdata.FileTypeName = ddlFileType.SelectedItem.ToString();
objdata.FileName = FileName;
objdata.FilePath = PathToDB;
objdata.FileTitle = txtTitle.Text == "" ? null : txtTitle.Text;
objdata.FileDecription = txtDecription.Text == "" ? null : txtDecription.Text;
List<FileUploaderData> DataList = new List<FileUploaderData>();
DataList = objBO.UpdateUploadFile(objdata);
if (DataList.Count > 0)
{
if (DataList[0].SQLOutput == 1)
{
lblUploadMsg.Visible = true;
lblUploadMsg.Text = "Uploaded Successfully.";
lblUploadMsg.Focus();
lblUploadMsg.CssClass = "SuccessAlert";
}
else if (DataList[0].SQLOutput == 2)
{
lblUploadMsg.Visible = true;
lblUploadMsg.Text = "Uploaded Update Successfully.";
lblUploadMsg.Focus();
lblUploadMsg.CssClass = "SuccessAlert";
}
}
else
{
lblUploadMsg.Focus();
lblUploadMsg.Visible = true;
lblUploadMsg.Text = "Not Uploaded due to system error.";
lblUploadMsg.CssClass = "FailedAlert";
}
}
}
catch (Exception ex)
{
HandlePolicyException.ExceptionHandler(HandlePolicyException.PolicyName.UIExceptionPolicy, ex, "1000001");
LogManager.LogError(ex, EnumErrorSource.Web);
MessageAlert_.ShowMessage(ex, "system", 0);
}
}
【问题讨论】:
-
如果你重构了那个怪物,可能会更容易识别错误
-
请添加更多信息,如行号、堆栈跟踪等。
-
确保您的生产帐户有权在其中创建新文件夹和文件。
-
你能帮我解决这个问题吗
-
我相信 NullReferenceException 意味着某处有 null。你调试你的代码了吗?至少您可以提供堆栈跟踪以获取更多信息