开发者

Object reference not set to an instance of an object #5

开发者 https://www.devze.com 2022-12-23 13:31 出处:网络
sUsername.Trim(); sPassword.开发者_运维知识库Trim(); string ConnectionString = WebConfigurationManager.ConnectionStrings[\"dbnameConnectionString\"].ConnectionString;
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.


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.


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.


Line 30 and 31 don't do anything:

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

Post where the error occurs


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号