Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL-I am getting following error for particular time period-like 6 am to 6:30 am-
The code is a part of a Service which is called by some job-Here one Store Proc is being called and connection to SQL DB getting failed at around 6:30 (for 10 mins).
So transaction is being rolled back at around 6:30 am.


C#
System.NullReferenceException: Object reference not set to an instance of an object.     at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)     at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)     at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)     at System.Data.SqlClient.SqlConnection.Open()     at Hallmark.ProducerPortal.WebServices.VantageBrokerService.SearchAgents(SearchAgentsRequest searchCriteria) in d:\TFS_RETAIL_Builds\31\VUI-RPP\PPWebServices_R22\src\WebServices\VantageBrokerService.asmx.cs:line 102 



After 6;30, the same code execute without any exception (though with less data)

What I have tried:

I have analyzed the DB, what are the other jobs, SQL script are running in the DB server which may consume much memory of the DB.I have stopped 2 jobs(which was consuming high CPU) which was running around 6:30 am . Still not working.
Posted
Updated 6-Jun-16 22:06pm
Comments
George Jonsson 6-Jun-16 19:04pm    
Don't repost your question, next time update the existing.
See Sql-connection timeout error for a particular period of time[^]

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown. And showing the exception stack is pretty much useless without showing the code where the exception is thrown. Rather, you, as you have everything, can figure out the problem.

Not to worry. This is one of the easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferences by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 
Comments
George Jonsson 6-Jun-16 19:01pm    
+5
Sergey Alexandrovich Kryukov 6-Jun-16 19:05pm    
Thank you, George.
—SA
Member 4228925 13-Jun-16 17:11pm    
Thank you SA... But problem is not consistent. At 6:30 am, when job is run, this problem comes and later point of time this problem does not comes. So i am searching for other possible reason- Like issue in the Store proc its calling, the load in the DB , or conflict with other call which is trying to fetch the same table - which is amounting to the null object. My main concern- if it is simple null reference issue, why i dont get it in other time frame?
Sergey Alexandrovich Kryukov 13-Jun-16 22:07pm    
Then it's worth. You really need to reproduce the problem. But did you observe it under the debugger at least once?
—SA
Member 4228925 15-Jun-16 16:41pm    
yes i did. I called the method of the Service 3000 times from SOAP UI, that mean 3000 the DB was also connected and Stopre proc was executed. Still no exception i found while i locally debugged the code.

SqlCommand cmd = SearchAgentsCommand(searchCriteria);
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
response.Agents = new List<contracts.agent>();
while (dr.Read())
{
Agent = GetAgent(dr, searchCriteria.SearchOnly);
response.Agents.Add(Agent);
}
dr.Close();

if (searchCriteria.SearchOnly == AgentSearchType.U65Agent)
{
foreach (Contracts.Agent agent in response.Agents)
{
agent.Languages = Utilities.GetAgentLanguages(agent.AgentId, con);
}
}

Problem happening at -foreach (Contracts.Agent agent in response.Agents)
So if response.Agents is null then -Null reference exception will come.
But response.Agents can not be null as its being initialized using- response.Agents = new List<contracts.agent>();
So i am clueless...
Adding one more point- There is also another similar job which also try to fetch data from same table. Not sure if there is any race condition or any similar issue or not.
Please check connection object. I faced same issue.
 
Share this answer
 
Comments
Member 4228925 13-Jun-16 17:11pm    
Thank you Tejas... But problem is not consistent. At 6:30 am, when job is run, this problem comes and later point of time this problem does not comes. So i am searching for other possible reason- Like issue in the Store proc its calling, the load in the DB , or conflict with other call which is trying to fetch the same table - which is amounting to the null object. My main concern- if it is simple null reference issue, why i dont get it in other time frame?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900