未将对象引用设置到对象的实例。string path = Server.MapPath("upLoad⼀") + Session["file"].ToString()

2025-06-22 18:46:26
推荐回答(4个)
回答1:

明显,你的session从哪里来?,很显然你没有赋值!
protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e)
{
Session["select"] = ListBox1.SelectedValue.ToString();
}
还有,你看你的网站前台有没有写这个事件:
onselectedindexchanged="ListBox1_SelectedIndexChanged1">

onselectedindexchanged="ListBox1_SelectedIndexChanged1这个东西如果在前台没写是不会报错的,不像其他空间那样。我也遇到同样的问题,不过现在解决了,是因为组心啊!!!

回答2:

Session["file"]为null造成转换成string的时候出错,建议判断一下:
if(Session["file"]!=null)
{
string path = Server.MapPath("upLoad/") + Session["file"].ToString();
}
else
{
Response.Write("Session未赋值!");
Response.End();
}

回答3:

string path = Server.MapPath("upLoad/") + Session["file"].ToString();
==
string path = Server.MapPath("~/upLoad/") + Session["file"].ToString();

回答4:

果断调试下 打断点-》F5 是因为哪个地方为空所致,调试找出来嘛