标题:用户代码NullReferenceException未将对象引用设置到对象的实例
只看楼主
董晓云
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-4-5
结帖率:42.86%
 问题点数:0 回复次数:5 
用户代码NullReferenceException未将对象引用设置到对象的实例
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace xueshengtonzhi
{
    public partial class detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack )
                DataGridDataBind();

        }
        protected void DataGridDataBind()
        {
            string ID = "";
            if (Request["ID"].ToString()!=null)
            {
                ID = Request["ID"].ToString();//取得上以页面传递过来的参数
            }
            
            SqlConnection conn = new SqlConnection("server=127.0.0.1;uid=sa;pwd=1234;database=xiwangzhan");
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from xstzb where id=" + ID, conn);
            //打开数据连接
            //创建数据读取器
            try
            {
                SqlDataReader dr = cmd.ExecuteReader();
                //如果取到数据
                if (dr.Read())
                {
                    head.Text = dr["Title"].ToString();
                    content.Text = dr["Content"].ToString();
                    man.Text = dr["Publicman"].ToString();
                    riqi.Text = Convert.ToDateTime(dr["Publictime"]).ToString();
                }

            }
            catch (Exception error)
            {
                Response.Write(error.ToString());
            }
            finally{
                //关闭数据连接
                conn.Close();
            }
        运行时出现户代码NullReferenceException未将对象引用设置到对象的实例,各位高手帮忙看看哪有问题?谢谢啦!
搜索更多相关主题的帖子: 实例 对象 代码 用户 
2010-04-22 09:23
yms123
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
得分:0 
            {
                SqlDataReader dr = cmd.ExecuteReader();
                //如果取到数据
                if (dr.Read())
                {
                    head.Text = dr["Title"].ToString();
                    content.Text = dr["Content"].ToString();
                    man.Text = dr["Publicman"].ToString();
                    riqi.Text = Convert.ToDateTime(dr["Publictime"]).ToString();
                }

            }
            catch (Exception error)
            {
                Response.Write(error.ToString());
            }
这段代码出的问题?
2010-04-22 21:25
misswang
Rank: 8Rank: 8
来 自:湖北荆州
等 级:贵宾
威 望:15
帖 子:265
专家分:720
注 册:2010-3-11
得分:0 
一、对象所在的命名空间没有引用

二、对象没有实例化

三、出现异常。实例化失败对象为 null

misswp2010@
wangping103@
2010-04-23 09:43
visolleon
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:71
专家分:182
注 册:2007-1-16
得分:0 
这程序:
1、安全问题:你获取form或者是QueryString的ID值,根本没经过任何判断就直接拼接SQL语句执行,这是最大的安全隐患。建议:整形先int.TryParse一下,非整形,判断敏感字符,或者写正则判断。
2、DataReader的效率很高,不过如果你在读取的时候操作很多内容,如你这样设置页面的控制值,这样很占用系统资源,很可能会发生问题,比如,DataReader超时了。建议:用实体类保存值,实在不行你用最简单的DataSet吧,虽然性能差了点。

以目前你发的程序,做完这两条,我相信你能解决你的问题了。

2010-04-25 19:02
wc1432
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:18
注 册:2010-4-8
得分:0 
4楼正解
2010-04-26 11:46
zzz2800
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-3-7
得分:0 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class student : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //数据库的连接
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.;Initial Catalog=usersinfo;Integrated Security=SSPI";
            //存储过程实例化
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            //选出符合SQL语句执行后的列的全部结果
             = "select * from users where uid = @id";
            cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.VarChar, 50));
            cmd.Parameters["@id"].Value = Session["uid"].ToString();
            conn.Open();
            //读取1行记录
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Label1.Text = dr[0].ToString();
                Label2.Text = dr[1].ToString();
                Label3.Text = dr[2].ToString();
                Label4.Text = dr[3].ToString();
                Label5.Text = dr[4].ToString();
                Label6.Text = dr[5].ToString();
                Label7.Text = dr[7].ToString();
                TextBox1.Text = dr[8].ToString();
                TextBox2.Text = dr[9].ToString();
                TextBox3.Text = dr[10].ToString();
            }
            conn.Close();
        }
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=usersinfo;Integrated Security=SSPI";
        conn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
         = "update users set uaddress='" + TextBox1.Text + "',uphone='" + TextBox2.Text + "',uemail='" + TextBox3.Text + "' where uid='" + Session["uid"].ToString() + "'";
        int i = cmd.ExecuteNonQuery();
        if (i == 0)
        {
            Response.Write("<script language='javascript'>alert('修改信息失败!');window.location.href='admin.aspx';</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('修改信息成功!');window.location.href='admin.aspx';</script>");
        }
        conn.Close();
    }
}
红色字体部分提示:用户代码未处理BullReferenceException,未将对象引用设置到对象实例
使用new关键字创建对象实例在调用方法前通过检查确定对象是否为空。
请问各位高手是怎么回事呢?求帮忙。
2012-03-07 10:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-303982-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.701666 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved