C# System.NullReferenceException: 未将对象引用设置到对象的实例

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Zhang_hongchao/article/details/71642156

使用ExecuteScalar(sql,entity,tran)添加数据时,数据表中必须要有自增长的id字段,不然会报错 System.NullReferenceException: 未将对象引用设置到对象的实例


第一次做C#项目遇上这个错误,和同学调试了一天,心累……

展开阅读全文

iis6.0 System.NullReferenceException: 未将对象引用设置对象实例

02-17

未将对象引用设置到对象的实例。 rn说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。rnrn源错误: rnrnrn行 122: rn行 123: //使用版块rn行 124: if ((config.Iisurlrewrite == 1 || config.Aspxrewrite == 1) && requestPath.EndsWith("/list.aspx") &&rn行 125: requestPath.IndexOf("/archiver/") < 0 && requestPath.IndexOf("/install/") < 0 && requestPath.IndexOf("/upgrade/") < 0 &&rn行 126: requestPath.IndexOf("/admin/") < 0 && requestPath.IndexOf("/aspx/") < 0 && requestPath.IndexOf("/tools/") < 0 &&rn rnrn源文件: D:\BBSProjects\XL.Forum\HttpModule.cs 行: 124 rnrn堆栈跟踪: rnrnrn[NullReferenceException: 未将对象引用设置到对象的实例。]rn XL.Forum.HttpModule.ReUrl_BeginRequest(Object sender, EventArgs e) in D:\BBSProjects\XL.Forum\HttpModule.cs:124rn System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +213rn System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +76rnrn rnrnrn--------------------------------------------------------------------------------rn版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1 rnrn疑惑:rn我在vs2008 调试的时候就没有这个异常,为什么部署到iis中就出现了这个异常呢?rn希望老师给我个解答。 论坛

System.NullReferenceException: 未将对象引用设置对象实例

12-22

错误详细:rnrn异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。rnrn源错误: rnrnrn行 13: rn行 14: MessageInterface iMsg = MessageFactory.Create();rn[color=#FF0000]rn行 15: return iMsg.AddMessage(message);rn[/color]rn行 16: rn行 17: rn rnrn源文件: F:\GameDesign\Bussiness\MessageBusiness.cs 行: 15 rnrnrn运行页面后台代码:rn[code=C#]rnusing System;rnusing System.Collections;rnusing System.Configuration;rnusing System.Data;rnusing System.Web;rnusing System.Web.Security;rnusing System.Web.UI;rnusing System.Web.UI.HtmlControls;rnusing System.Web.UI.WebControls;rnusing System.Web.UI.WebControls.WebParts;rnusing System.IO;rnusing GD.Model;rnusing GD.DataAccess;rnusing GD.SystemFrameworks;rnusing GD.Bussiness.Message;rnrnpublic partial class AddMsg : System.Web.UI.Pagernrn protected void Page_Load(object sender, EventArgs e)rn rn if (Session["fld_UserName"] == null)rn rn this.Response.Write(" ");rn Response.Redirect("Login.aspx?message=" + Server.UrlEncode("请登录后再留言!"));rn rn //if (!CommonClass.hasSession(Page.Session))rn // Response.Redirect("Login.aspx?message=" + Server.UrlEncode("请登录后再留言!"));rn rnrn protected void btnOK_Click(object sender, EventArgs e)rn rnrn Message message = new Message();rn message.fld_UserId = Convert.ToInt32(Label1.Text);rn message.fld_Title = txtTitle.Text;rn message.fld_Content = txtDSC.Text;rn message.fld_CreatedTime = Convert.ToDateTime(DateTime.Now);rnrn MessageBusiness messageBusiness = new MessageBusiness();rn if (messageBusiness.AddMessage(message))rn rn Utility.AlertMsg(this.Page, "成功");rn rn elsern rn Utility.AlertMsg(this.Page, "失败");rn rn rnrn[/code]rnrn业务层代码:rn[code=C#]rnnamespace GD.Bussiness.Messagernrn using System;rn using System.Data;rn using System.Collections.Generic;rn using System.Text;rn using GD.Model;rn using GD.DataAccess.Message;rnrn public class MessageBusinessrn rn public bool AddMessage(Message message)rn rn MessageInterface iMsg = MessageFactory.Create();rn return iMsg.AddMessage(message);rn rn rnrn[/code]rnrn数据处理代码:rn[code=C#]rnnamespace GD.DataAccess.Messagernrn using System;rn using System.Data;rn using System.Data.SqlClient;rnrn using GD.Model;rn using GD.SystemFrameworks;rn using GD.DataAccess.SystemFrame;rnrn public class MessageSqlHandle : MessageInterfacern rn public bool AddMessage(Message message)rn rn SQLHelper helper = new SQLHelper();rn SqlParameter[] prams = rn new SqlParameter("@fld_UserId",SqlDbType.Int,8),rn new SqlParameter("@fld_Title",SqlDbType.NVarChar,50),rn new SqlParameter("@fld_Content",SqlDbType.NVarChar),rn new SqlParameter("@fld_CreatedTime",SqlDbType.DateTime,8)rn ;rn prams[0].Value = message.fld_UserId;rn prams[1].Value = message.fld_Title;rn prams[2].Value = message.fld_Content;rn prams[3].Value = message.fld_CreatedTime;rnrn int i;rn i = helper.ExecuteNonQuery("Tb_AddMessage", prams);rn if (i == 0)rn return true;rn elsern return false;rn rn rnrn[/code]rn存储过程:rn[code=SQL]rnALTER PROCEDURE dbo.Tb_AddMessagern (rn @fld_UserId int,rn @fld_Title nvarchar(100),rn @fld_Content nvarchar(max),rn @fld_CreatedTime datetime,rn @fld_Clicks intrn )rnASrnBEGINrn BEGIN TRYrn INSERT INTO Tb_Message(rn fld_UserId,rn fld_Title,rn fld_Content,rn fld_CreatedTime,rn fld_Clicksrn )VALUES(rn @fld_UserId,rn @fld_Title,rn @fld_Content,rn @fld_CreatedTime,rn @fld_Clicksrn )rn rn RETURN 0rn END TRYrn BEGIN CATCHrn RETURN ERROR_NUMBER()rn END CATCHrnrnENDrn[/code]rnrn工厂反射模式 的程序 折腾了一下午了 始终解决不了 急啊~~~rn 论坛

异常!System.NullReferenceException: 未将对象引用设置对象实例

04-29

[code=csharp] rn//========================= ItemDTO 定义 rnpublic class ItemDTOrn rn public int id;rn public int itemId;rn public int itemNum;rn public string outTime;rn public int roleId;rn public int storageType;rn rn//========================= GardenSeed 定义 rnpublic class GardenSeed : ObjBasern rn public int SeedTimern rn getrn rn return Convert.ToInt32(base.Columns["SeedTime"].Value);rn rn setrn rn base.Columns["SeedTime"].Value = value;rn rn rn public string SeedGetMoneyrn rn getrn rn return Convert.ToString(base.Columns["SeedGetMoney"].Value);rn rn setrn rn base.Columns["SeedGetMoney"].Value = value;rn rn rn public int IDrn rn getrn rn return Convert.ToInt32(base.Columns["ID"].Value);rn rn setrn rn base.Columns["ID"].Value = value;rn rn rn public int SeedIDrn rn getrn rn return Convert.ToInt32(base.Columns["SeedID"].Value);rn rn setrn rn base.Columns["SeedID"].Value = value;rn rn rn public GardenSeed()rn rn base.TableName = "GardenSeed";rn base.AddColumn(new Column("SeedTime", ColumnType.Int));rn base.AddColumn(new Column("SeedGetMoney", ColumnType.String));rn base.AddColumn(new Column("ID", "0", ColumnType.Int, true, IdentityType.Normal));rn base.AddColumn(new Column("SeedID", ColumnType.Int));rn rn rn//========================= 出错地方rnpublic class PackagMgr : BaseMgrrn rn public static List CURRENT_PACAGE = new List();rn private MyGM myGM_0 = GM.CreateNewGM();rn private ResourceMgr resourceMgr_0 = new ResourceMgr();rn public void Test()rn rn ResourceMgr resourceMgr = new ResourceMgr();rn foreach (ItemDTO current in PackagMgr.CURRENT_PACAGE)rn rn Console.WriteLine(string.Concat(new object[]rn rn current.itemId,rn ":",rn resourceMgr.GetItemByItemID(current.itemId).MyName,rn ":",rn current.itemNum,rn " st:",rn current.storageTypern ));rn rn rnrn public ItemDTO method_0(int id)rn rn ItemDTO result;rnrn for (int i = 0; i < PackagMgr.CURRENT_PACAGE.Count; i++)//出错rn rnrn if (PackagMgr.CURRENT_PACAGE[i].id == id)rn rn result = PackagMgr.CURRENT_PACAGE[i];rn return result;rn rn rnrn result = null;rn return result;rn rnrn public ItemDTO GetItemByItemID(int id)rn rn ItemDTO result;rn for (int i = 0; i < PackagMgr.CURRENT_PACAGE.Count; i++)//出错rn rnrn if (PackagMgr.CURRENT_PACAGE[i].itemId == id)rn rn result = PackagMgr.CURRENT_PACAGE[i];rn return result;rn rnrn rn result = null;rn return result;rnrn//=================调用rnforeach (EquipDTO current in DataCenter.CURRENT_EQUIPS)rn rn string[] array = new string[4];rn ItemDTO itemDTO = packagMgr.method_0(current.id);//调用method_0rnif (itemDTO != null)rnrn Item itemByItemID = resourceMgr.GetItemByItemID(itemDTO.itemId);rn//调用GetItemByItemIDrn if (itemByItemID != null)rn rnrn array[0] = itemByItemID.MyName;rnrn rn rnrn//------调用rnprivate void method_15()rn rn this.util_0.ClearLV(this.lv_seed);rn ResourceMgr resourceMgr = new ResourceMgr();rn List gardenSeed = resourceMgr.GetGardenSeed();rn if (gardenSeed != null && gardenSeed.Count != 0)rn rn List list = new List();rn PackagMgr packagMgr = new PackagMgr();rn foreach (GardenSeed current in gardenSeed)rn rn string[] array = new string[4];rn array[0] = resourceMgr.GetItemNameByItemID(current.SeedID);rn array[1] = current.SeedTime.ToString() + "天";rn array[2] = "0";rnrn ItemDTO itemByItemID = packagMgr.GetItemByItemID(current.SeedID);rn //调用GetItemByItemID rn if (itemByItemID != null)rn rn array[2] = itemByItemID.itemNum.ToString();rn rn array[3] = current.SeedID.ToString();rn list.Add(array);rn[/code]rnrnrnrnrnrnrn[]未处理异常!System.NullReferenceException: 未将对象引用设置到对象的实例。rn 在 Manager.PackagMgr.GetItemByItemID(Int32 id) 位置 E:\工程\PackagMgr.cs:行号 60rn百度好久 发现出现这个问题都是 未初始化 和 NULL值rnpublic static List CURRENT_PACAGE后面加上了new List();rn还是不行rn不知道怎么弄啊 求各位大哥帮帮忙 论坛

没有更多推荐了,返回首页