未将对象引用设置为对象的实例 #5

发布于 2024-08-25 21:33:45 字数 438 浏览 3 评论 0原文

sUsername.Trim();
sPassword.Trim();
string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);

未将对象引用设置为对象的实例。 描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

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

有什么想法吗?我不明白这个错误。

sUsername.Trim();
sPassword.Trim();
string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Any ideas? I don't understand the error.

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

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

发布评论

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

评论(5

口干舌燥 2024-09-01 21:33:45

好吧,你还没有显示它出现在哪一行。它表明发生了其中一种情况:

  • sUsername 为 null
  • sPassword 为 null
  • WebConfigurationManager.ConnectionStrings["dbnameConnectionString"] returned null

Btw,调用 Trim() 作为一个单独的语句是毫无意义的。字符串是不可变的 - Trim() 返回修剪后的版本。你想要类似的东西:

sUsername = sUsername.Trim();
sPassword = sPassword.Trim();

...但只有在检查它们是否为空之后。

Well, you haven't shown which line it occurs on. It suggests that one of these occurred:

  • sUsername was null
  • sPassword was null
  • WebConfigurationManager.ConnectionStrings["dbnameConnectionString"] returned null

Btw, calling Trim() as a statement on its own like that is pointless. Strings are immutable - Trim() returns the trimmed version. You want something like:

sUsername = sUsername.Trim();
sPassword = sPassword.Trim();

... but only after checking whether they're null or not.

姐不稀罕 2024-09-01 21:33:45

嗯,我确实明白,但你错过了行参考。错误发生在哪里?

Line 30:         sUsername.Trim();
Line 31:         sPassword.Trim();
Line 32:         string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
Line 33:         SqlConnection myConnection = new SqlConnection(ConnectionString);
Line 34:         try

如果我假设 sPassword 存在 - 和 sUsername...
...那么ConnectionString“dbNameConnectionString”是否存在于web.config中?如果不是,则为 null,并且“.ConnectionString”自然会抛出该错误。

Well, I do understand it, but you miss line references. Where does the error occur?

Line 30:         sUsername.Trim();
Line 31:         sPassword.Trim();
Line 32:         string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
Line 33:         SqlConnection myConnection = new SqlConnection(ConnectionString);
Line 34:         try

if I assume that sPassword exists - and sUsername...
...then does the ConnectionString "dbNameConnectionString" exist in the web.config? If not- that is null, and the ".ConnectionString" naturally throws that error.

﹏半生如梦愿梦如真 2024-09-01 21:33:45

第 30 行和第 31 行不执行任何操作:

sUsername = sUsername.Trim();
sPassword= sPassword.Trim();

发布错误发生的位置

Line 30 and 31 don't do anything:

sUsername = sUsername.Trim();
sPassword= sPassword.Trim();

Post where the error occurs

月依秋水 2024-09-01 21:33:45

发生这种情况是因为任何一个变量为 NULL。您可以在调试(运行时)期间检查 sUserNamesPassword 变量的值。

It happens because of any one of the variable is NULL. You can check the value of sUserName and sPassword variables during debugging (runtime).

坚持沉默 2024-09-01 21:33:45

这仅仅意味着您正在尝试访问空引用的成员;即这里的变量之一是null。在不知道行号的情况下,很难说是哪个,但我猜测是 sUsernamesPassword

This just means that you're trying to access a member of a null reference; i.e. one of the variables here is null. Without knowing the line number it's difficult to say which, but I'd guess at either sUsername or sPassword.

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