添加时出现空引用异常

发布于 2024-10-04 00:18:45 字数 2077 浏览 0 评论 0原文

今天晚上我发现了 SubSonic,经过一番使用它和 MySQL Connector 版本后,我终于让它接受我的连接字符串,设置了一个简单的存储库。

所以我创建了一个简单的类,然后去镇上向该类添加一些数据,然后创建一个存储库,并尝试保存记录。

时,我似乎只能摆脱空异常错误:

当我调用repo.Add(user);

但不知道什么是 null,并且错误也不是很清楚。

有一次,我将 User 类中 ID 字段的类型更改为 long,并从 SubSonic 中得到另一个错误(说它无法分辨哪个字段是我的 ID 字段,告诉我将其命名为 ID 或放置一个标签)上,但它已经被命名为 ID)

我已将数据放入类中的每个字段中,甚至是数据库不需要的字段,但什么也没有。

我花了晚上剩下的时间在谷歌上搜索有关我的错误的信息,但没有运气,而且网站上的文档也没有给我任何帮助。

知道我做错了什么,或者我如何诊断这个问题?

预先感谢您的任何帮助...

这是异常详细信息

System.NullReferenceException 未处理 消息=未将对象引用设置为对象的实例。 来源=MySql.Data 堆栈跟踪: 在 MySql.Data.MySqlClient.MySqlConnection.get_ServerThread() 在 MySql.Data.MySqlClient.MySqlConnection.Abort() 在MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior行为) 在MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior行为) 在 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior 行为) 在 SubSonic.DataProviders.DbDataProvider.ExecuteReader(QueryCommand qry) 在 SubSonic.Query.Insert.ExecuteReader() 在 SubSonic.Repository.SimpleRepository.Add[T](T 项) 在 C:\Users\Michaelm\Desktop\Subsonic_Demo\gc_ss\gc_ss\Program.cs 中的 gc_ss.Program.Main(String[] args) 处:第 27 行 在 System.AppDomain._nExecuteAssembly(程序集,String[] args) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,字符串 [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:

另一个更新

我注意到,如果我更改类(用户)并修改其中一个属性,则属性更改会反映在数据库中。另外,如果我删除该表,它会根据我的类构建。所以迁移部分正在发挥作用,这是之后发生的事情。

最终更新

好的,我删除了表,并将类更改为使用 long 作为 ID 字段。设置为RunMigrations,再次运行,失败,因为ID没有默认值。我将 MySQL 中的列设置为自动增量,现在一切正常。不确定它与昨天将其设置为 long 有什么不同(除非更改没有像我想象的那样被应用)。

无论如何,所以我想这个问题已经结束了......

I discovered SubSonic this evening, and after a little bit of playing with it and MySQL Connector versions, I finally got it to accept my connection string setting up a simple repository.

So I created a simple class, and went to town adding some data to the class then creating a repo, and trying to save the record.

All I can seem to get out of it is null exception errors when I call:

repo.Add(user);

No idea what's null though, and the error isn't very clear.

At one point, I changed the type of my ID field in my User class to a long and got another error out of SubSonic (saying it couldn't tell what field was my ID field, telling me to name it ID or put a tag on it, but it was already named ID)

I've put data in every field in the class, even those that are not required per the database, but nothing.

I spent the rest of the evening googling for information on my error, but no luck, and the documentation on the site didn't get me anywhere either.

Any idea what I'm doing wrong, or how I could possibly diagnose this?

Thanks in advance for any help...

Here's the exception details

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()
at MySql.Data.MySqlClient.MySqlConnection.Abort()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at SubSonic.DataProviders.DbDataProvider.ExecuteReader(QueryCommand qry)
at SubSonic.Query.Insert.ExecuteReader()
at SubSonic.Repository.SimpleRepository.Add[T](T item)
at gc_ss.Program.Main(String[] args) in C:\Users\Michaelm\Desktop\Subsonic_Demo\gc_ss\gc_ss\Program.cs:line 27
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Another Update

I'm noticing that if I change the class (User) and modify one of the properties, that the property changes get reflected in the database. Also if I drop the table, it gets constructed based on my Class. So the migrations part is working, it's something happening after that.

Final Update

Okay, I deleted the table, and changed the class to use a long as the ID field. Set to RunMigrations, and ran again, and it failed because of no default value for ID. I set the column in MySQL to AutoIncrement and all is working fine now. Not sure how it's different than setting it to long yesterday (unless changes weren't getting applied like I thought they were).

Anyway, so I guess this question is closed...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2024-10-11 00:18:45

这是 mysql.data 提供程序的问题。通常,连接器会抛出有意义的异常,但在此之前本身会出现内部空引用异常。

看看我的答案:Subsonic 3.0.0.4 MySQL ActiveRecord 模板插入新记录时出错
这解释了如何解决这个问题。

This is a issue with the mysql.data provider. Normally the connector would throw a meaningful exception but has an internal nullreference exception itself before that happens.

Look at my answer here: Subsonic 3.0.0.4 ActiveRecord Template for MySQL error on inserting new record
That explains how to work around that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文