回 帖 发 新 帖 刷新版面

主题:未将对象引用设置到对象的实例

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

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

源错误:


行 67:             }
行 68:
行 69:             if(UpLoadFile.PostedFile.ContentLength > 0)
行 70:             {
行 71:                 string FileName = UpLoadFile.PostedFile.FileName.Substring(UpLoadFile.PostedFile.FileName.LastIndexOf("\\"),


源文件: d:\news\admin\news_admin.aspx.cs    行: 69

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
   News.admin.News_Admin.Sub_Button_Click(Object sender, EventArgs e) in d:\news\admin\news_admin.aspx.cs:69
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277


这是全部的错误信息,下面是代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
//using System

namespace News.admin
{
    /// <summary>
    /// News_Admin 的摘要说明。
    /// </summary>
    public class News_Admin : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox News_Title;
        protected System.Web.UI.WebControls.TextBox News_Content;
        protected System.Web.UI.WebControls.Button Button2;
        protected System.Web.UI.WebControls.Button Sub_Button;
        protected System.Web.UI.HtmlControls.HtmlInputFile UpLoadFile;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.Sub_Button.Click += new System.EventHandler(this.Sub_Button_Click);
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button2_Click(object sender, System.EventArgs e)
        {
            News_Title.Text = " ";
            News_Content.Text = " ";
        }

        private void Sub_Button_Click(object sender, System.EventArgs e)
        {
            if(News_Title.Text.Trim().Length>0 && News_Content.Text.Trim().Length>0)
            {
                Response.Write("<script>alert(\"标题和内容不能为空\")</script>");
            }

            if(UpLoadFile.PostedFile.ContentLength > 0)
            {
                string FileName = UpLoadFile.PostedFile.FileName.Substring(UpLoadFile.PostedFile.FileName.LastIndexOf("\\"),
                    UpLoadFile.PostedFile.FileName.Length - UpLoadFile.PostedFile.FileName.LastIndexOf("\\"));

                if(File.Exists(Server.MapPath(Request.ApplicationPath) + "UpLoadFile\\" + FileName) == false)
                {
                    UpLoadFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "UpLoadFile\\" + FileName);
                    String ConnectionString = "data source=JINN; database=News; uid=sa; pwd=muyomima;";
                    SqlConnection connection = new SqlConnection(ConnectionString);
                    string cmdText = "insert into News (News_Title, News_Content, News_IMG) values('"+ News_Title.Text.Trim() +"', '"+
                        News_Content.Text.Trim() +"', '"+ "UpLoadFile" + FileName +"')";
                    SqlCommand command = new SqlCommand(cmdText, connection);
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                }
                else
                {
                    Response.Write("<script>alert(\"请重新命名,该文件名已经存在\")</script>");
                }
            }
            else
            {
                Response.Write("<script>alert(\"请选则文件路径\")</script>");
            }
        }
    }
}
希望看到的各位帮帮忙,我怎么也找不到错误的根源~~~
真是怪了啊~~~

回复列表 (共3个回复)

沙发

可能你的某个控件没设计好,你仔细检查一下,你的代码太多看不出来

板凳

UploadFile没有初始化

3 楼

你传递的参数不对
可能那个参数里就没有东西
或者在它之前就出现了错误但是没有显示
而在这里显示了错误

我来回复

您尚未登录,请登录后再回复。点此登录或注册