未将对象引用设置为对象的实例 #5
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你还没有显示它出现在哪一行。它表明发生了其中一种情况:
sUsername
为 nullsPassword
为 nullWebConfigurationManager.ConnectionStrings["dbnameConnectionString"]
returned nullBtw,调用
Trim()
作为一个单独的语句是毫无意义的。字符串是不可变的 -Trim()
返回修剪后的版本。你想要类似的东西:...但只有在检查它们是否为空之后。
Well, you haven't shown which line it occurs on. It suggests that one of these occurred:
sUsername
was nullsPassword
was nullWebConfigurationManager.ConnectionStrings["dbnameConnectionString"]
returned nullBtw, calling
Trim()
as a statement on its own like that is pointless. Strings are immutable -Trim()
returns the trimmed version. You want something like:... but only after checking whether they're null or not.
嗯,我确实明白,但你错过了行参考。错误发生在哪里?
如果我假设 sPassword 存在 - 和 sUsername...
...那么ConnectionString“dbNameConnectionString”是否存在于web.config中?如果不是,则为 null,并且“.ConnectionString”自然会抛出该错误。
Well, I do understand it, but you miss line references. Where does the error occur?
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.
第 30 行和第 31 行不执行任何操作:
发布错误发生的位置
Line 30 and 31 don't do anything:
Post where the error occurs
发生这种情况是因为任何一个变量为 NULL。您可以在调试(运行时)期间检查
sUserName
和sPassword
变量的值。It happens because of any one of the variable is NULL. You can check the value of
sUserName
andsPassword
variables during debugging (runtime).这仅仅意味着您正在尝试访问空引用的成员;即这里的变量之一是
null
。在不知道行号的情况下,很难说是哪个,但我猜测是sUsername
或sPassword
。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 eithersUsername
orsPassword
.