如何解决此异常(System.NullReferenceException:对象引用未设置为对象的实例)

时间:2019-07-01 06:47:48

标签: c# asp.net

将图像插入文件输入并按下添加按钮后,我得到了以上异常作为错误。

//Inserting image
//Image 01
string fileName1 = Path.GetFileNameWithoutExtension(tokenModel.ImageFile1.FileName);
string extension1 = Path.GetExtension(tokenModel.ImageFile1.FileName);
fileName1 = fileName1 + DateTime.Now.ToString("yymmssffff") + extension1;
tokenModel.ImagePath1 = "~/ProblemImages/" + fileName1;
fileName1 = Path.Combine(Server.MapPath("~/ProblemImages/"), fileName1);
tokenModel.ImageFile1.SaveAs(fileName1);

string imgPath1 = fileName1;


//Image 02
string fileName2 = Path.GetFileNameWithoutExtension(tokenModel.ImageFile2.FileName);
string extension2 = Path.GetExtension(tokenModel.ImageFile2.FileName);
fileName2 = fileName2 + DateTime.Now.ToString("yymmssffff") + extension2;
tokenModel.ImagePath2 = "~/ProblemImages/" + fileName2;
fileName2 = Path.Combine(Server.MapPath("~/ProblemImages/"), fileName2);
tokenModel.ImageFile2.SaveAs(fileName2);


string imgPath2 = fileName2;

错误消息:

  

异常详细信息: System.NullReferenceException
  对象引用未设置为对象的实例。

     

源错误:

     

第88行:
  字符串fileName1 = Path.GetFileNameWithoutExtension(tokenModel.ImageFile1.FileName);
  89行:
  字符串扩展1 = Path.GetExtension(tokenModel.ImageFile1.FileName);
  90行:
  fileName1 = fileName1 + DateTime.Now.ToString(“ yymmssffff”)+ extension1;

     

源文件:D:\ Visual Studio 2019   项目\ ISSCS_V_1.2 \ ISSCS_V_1.2 \ Controllers \ TokenController.cs

0 个答案:

没有答案