phone_m2_note
black-han
2016-06-04 18:24
采纳率: 100%
浏览 1.9k
已采纳

未将对象引用设置到对象的实例--新手,求大神解答!

新手,求大神解答!
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

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

源错误:

行 58: string txt = ftb.Text;
行 59: string newstime = Request.Form["newstime"];
行 60: string txt1 = newstime.Substring(0, 4) + "/" + newstime.Substring(5, 1) + "/" + newstime.Substring(8, 1);
行 61: string strInsert = "insert into news_article(bigclassid,title,writer,newskey,newstime,content,newsfrom)values('" + Request.Form["bigclass"] + "','" + Request.Form["title1"].Trim() + "','" + Request.Form["writer1"].Trim() + "','" + Request.Form["newskey1"].Trim() + "','" + txt1 + "','" + txt + "','" + Request.Form["newsfrom1"].Trim() + "')";
行 62: SqlCommand commInsert = new SqlCommand(strInsert, conn);

源文件: c:\Users\123\第11章\add_article.aspx.cs 行: 60

堆栈跟踪:

[NullReferenceException: 未将对象引用设置到对象的实例。]
add_article.Button1_Click(Object sender, EventArgs e) in c:\Users\123\第11章\add_article.aspx.cs:60
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9692746
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562

代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class add_article : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null || Session["password"] == null)
{
Response.Redirect("login.aspx");
}
string rqid = Request.QueryString["newid"];
if (rqid != null)
{
Button1.Visible = false;
string strSql = "select newid,bigclassname,title,newsfrom,newstime,writer,hit,newskey,content from news_article inner join news_bigclass on news_article.bigclassid=news_bigclass.bigclassid where newid='" + rqid + "' order by newstime desc";
conn.Open();
SqlCommand comm = new SqlCommand(strSql, conn);
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
title1.Text = dr["title"].ToString();
newskey1.Text = dr["newskey"].ToString();
newsfrom1.Text = dr["newsfrom"].ToString();
writer1.Text = dr["writer"].ToString();
ftb.Text = dr["content"].ToString();
newstime.Text = dr["newstime"].ToString();
}
conn.Close();
}
else
{
newstime.Text = DateTime.Now.ToString();
Button2.Visible = false;
}
string strLeiBie = "select * from news_bigclass";
conn.Open();
SqlCommand commLeiBie = new SqlCommand(strLeiBie, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = commLeiBie;
DataSet ds = new DataSet();
da.Fill(ds, "leibie");
bigclass.DataSource = ds.Tables["leibie"];
bigclass.DataBind();
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
conn.Open();
string txt = ftb.Text;
string newstime = Request.Form["newstime"];
string txt1 = newstime.Substring(0, 4) + "/" + newstime.Substring(5, 1) + "/" + newstime.Substring(8, 1);
string strInsert = "insert into news_article(bigclassid,title,writer,newskey,newstime,content,newsfrom)values('" + Request.Form["bigclass"] + "','" + Request.Form["title1"].Trim() + "','" + Request.Form["writer1"].Trim() + "','" + Request.Form["newskey1"].Trim() + "','" + txt1 + "','" + txt + "','" + Request.Form["newsfrom1"].Trim() + "')";
SqlCommand commInsert = new SqlCommand(strInsert, conn);
commInsert.ExecuteNonQuery();
conn.Close();
Response.Write("alert('成功添加新闻信息!');windows.history.go(-1);");
}
protected void Button2_Click(object sender, EventArgs e)
{
conn.Open();
string txt = ftb.Text;
string newstime = Request.Form["newstime"];
string txt1 = newstime.Substring(0, 4) + "/" + newstime.Substring(5, 1) + "/" + newstime.Substring(8, 1);
string strUpdate = "update news_article set title='" + Request.Form["title1"].Trim() + "',bigclassid='" + Request.Form["bigclass"] + "',writer='" + Request.Form["writer1"].Trim() + "',newskey='" + Request.Form["newskey1"].Trim() + "',newsfrom='" + Request.Form["newsfrom1"].Trim() + "',newstime='" + txt1 + "',content='" + txt + "'where newid='" + Request.QueryString["newid"] + "'";
SqlCommand commUpdate = new SqlCommand(strUpdate, conn);
commUpdate.ExecuteNonQuery();
conn.Close();
Response.Write("alert('成功修改新闻信息!');windows.history.go(-1);");
}
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • zhaoyu16
    zhaoyu16 2016-06-05 05:44
    已采纳

    这还要搞这么复杂...
    第60行newstime为null,原因是59行的Request.Form["newstime"]没有取到对应参数的值

    点赞 评论
  • caozhy

    Request.Form["newstime"]这个没有取到值,检查下表单名字是否正确。

    点赞 评论
  • chx10051413
    北京-星辰 2016-06-05 00:13

    这种问题自己仔细调试下就可以了

    点赞 评论
  • shadow_dreamer
    shadow_dreamer 2016-06-05 00:37

    这种问题,进入调试模式下,运行过程中会自动定位到异常处,再查看详细的信息,就容易多咯。

    点赞 评论
  • tr1912
    王啸tr1912 2016-06-05 02:06

    单步调试一下,应该是调用的某个变量或者对象是null,仔细看一下赋值,实例化对象的时候哪里出问题了

    点赞 评论

相关推荐