System.NullReferenceException : Object reference not set to an instance of an object.
-
Friday, February 26, 2010 11:58 PM
I am creating unit testing for my website.
The web site is using VB. I publish this website and get App_Code.dll and bunch parts of dll files. I also sign a strong name for these assembly.
In my unit testong project, I use c#, add a reference of the App_Code.dll.
I create a unit testing
using System; using NUnit.Framework; using System.Xml; using Rhino.Mocks; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Linq; using System.Collections; using System.Collections.Generic; using System.EnterpriseServices; namespace wwwrootNunitTest { [TestFixture] [Transaction(TransactionOption.Required)] public class cAdminCommonTest: ServicedComponent { string conn; [SetUp] public void Init() { conn = "Server=localhost;Database=xyz;Trusted_Connection=True"; } [TearDown] public void TransactionTearDown() { if (ContextUtil.IsInTransaction) { ContextUtil.SetAbort(); } } [Test] public void FireQueryIdentityTest_insertAndGetInsertIdentity() { string sqlInsert = "insert tbl_source_control_msg (message, tested, submited_by, revision_num) values('insert test', 1,'lynda',7)"; string paraName = "@id"; int expected = 75; int actual; cAdminCommon target = new cAdminCommon(); actual = target.FireQueryIdentity(sqlInsert, paraName); Assert.AreEqual(actual, expected); } } }
When I start run it, I get error
wwwrootNunitTest.cAdminCommonTest.FireQueryIdentityTest_insertAndGetInsertIdentity:
System.NullReferenceException : Object reference not set to an instance of an object.
The trace is this:
Server stack trace:
at cAdminCommon..ctor()
at wwwrootNunitTest.cAdminCommonTest.FireQueryIdentityTest_insertAndGetInsertIdentity() in C:\Users\p1943fm\Documents\Visual Studio 2008\Projects\Working_copy\wwwrootNunitTest\wwwrootNunitTest\cAdminCommonTest.cs:line 62
at System.Runtime.Remoting.Messaging.Message.Dispatch(Object target, Boolean fExecuteInContext)
at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at wwwrootNunitTest.cAdminCommonTest.FireQueryIdentityTest_insertAndGetInsertIdentity()
I searched and found that most problem is caused by not use New to initiate an object. However, i do havecAdminCommon
target = new cAdminCommon();
Actually the error happens at this very point. The class cAdminCommon has constructor like
Public Class cAdminCommon Implements IAdminCommon Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Dim conn As SqlConnection Public Sub New() End Sub Public Sub New(ByVal Iadmin As IAdminCommon) Dim admin As IAdminCommon admin = Iadmin Me.conn = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) End SubI added IAdminCommon interface to it since I'd like to use some mocks. Nearly all examples I got uses interface, though my class cAdminCommon does not have one.
Your help will be greatly appreciated.
Best Ref=gards
Lynda
All Replies
-
Tuesday, March 02, 2010 11:45 PMHi there,
Have you tried debugging into the constructor itself to try to see exactly where the exception is being thrown?
Thanks,
David Gorena Elizondo
[MSFT] Visual Studio Team Test -
Wednesday, March 03, 2010 7:29 PM
I guess what throws is the line "Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)" because you don't have a connection string in your configuration. The thing is that all member initializers are called as it they were in the beginning of the constructor. To prove that you can change it to "Dim conn As SqlConnection" and run the test again.
Thanks,
Michael Koltachev
Visual Studio Team Test- Proposed As Answer by Michael Koltachev - MSFT Wednesday, March 03, 2010 7:30 PM
- Marked As Answer by Lynda Li Thursday, March 04, 2010 12:01 AM
-
Thursday, March 04, 2010 12:01 AMYou are right. It is the problem of calling Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString). I think the web.config file cannot be accessed by my unit testing project. Thank you very much!
Lynda -
Monday, May 10, 2010 2:57 PM
Hi,
I am having a similar problem but I cannot paste the code here. Were you able to fix the problem w/o modifying the code? What did you do to make the Web.Config available to the calling program (exe in my case)?
-
Monday, May 10, 2010 6:07 PMThe change I made is convert my web site project to web application project. At first I tried to put connection string in resource file, however, Web Site project does not have single .dll, it made testing very difficult. At the end I have to change to it to Web Application Project to produce a single .dll. It is a lot of work, but it made accessing web.config problem go away.

