Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 135693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:49:51+00:00 2026-05-11T06:49:51+00:00

Ok, I’ve got a strange problem. I am testing a usercontrol and have code

  • 0

Ok, I’ve got a strange problem. I am testing a usercontrol and have code like this:

[TestFixture] public myTestClass : UserControl {     MyControl m_Control;      [Test]     public void TestMyControl()     {         m_Control = new MyControl();         this.Controls.Add(m_Control);          Assert.That(/*SomethingOrOther*/)     } } 

This works fine, but when I change it to:

[TestFixture] public myTestClass : UserControl {     MyControl m_Control;      [Setup]     public void Setup()     {         m_Control = new MyControl();         this.Controls.Add(m_Control);     }      [TearDown]     public void TearDown()     {         this.Controls.Clear();     }      [Test]     public void TestMyControl()     {         Assert.That(/*SomethingOrOther*/);     } } 

I get an Object Reference Not Set To An Instance of an Object. I even output to the console to ensure that the setup/teardown were running at the correct times, and they were… but still it isn’t newing up the usercontrols.

edit> The exact code is:

[TestFixture] public class MoneyBoxTests : UserControl {     private MoneyBox m_MoneyBox;     private TextBox m_TextBox;      #region 'Setup/TearDown'     [SetUp]     public void Setup()     {         MoneyBox m_MoneyBox = new MoneyBox();         TextBox m_TextBox = new TextBox();          this.Controls.Add(m_MoneyBox);         this.Controls.Add(m_TextBox);     }      [TearDown]     public void TearDown()     {         this.Controls.Clear();     }     #endregion      [Test]     public void AmountConvertsToDollarsOnLeave()     {         m_MoneyBox.Focus();         m_MoneyBox.Text = '100';         m_TextBox.Focus();          Assert.That(m_MoneyBox.Text, Is.EqualTo('$100.00'), 'Text isn't $100.00');     }      [Test]     public void AmountStaysANumberAfterConvertToDollars()     {         m_MoneyBox.Focus();         m_MoneyBox.Text = '100';         m_TextBox.Focus();          Assert.That(m_MoneyBox.Amount, Is.EqualTo(100), 'Amount isn't 100');     } } 

I get the exception(s) at the respective m_MoneyBox.Focus() calls.

Solved – See Joseph’s comments

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:49:52+00:00Added an answer on May 11, 2026 at 6:49 am

    I created a test case with exactly the same layout you presented here, but with a TextBox instead of a MyControl. I also added a constructor and a deconstructor and outputted all the various stages to the console to see the sequence of events. However, I never got an object reference exception.

    In case you are interested, the sequence was [constructor called], [setup called], [test called], [tear down called]. The deconstruction never output anything to the screen for some reason.

    My original thought was that the Controls property on myTestClass would not be initialized, but on my test it was, so I think it has something to do with your MyControl construction.

    edit> I added the focus on my TextBox in my unit test as well but still no exception. Does your MoneyBox have any event handling going on behind the scenes during the Focus? That might be your culprit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try something like this: Dim TestToSplit As String = "Foo"",""Bar"… May 12, 2026 at 10:04 am
  • Editorial Team
    Editorial Team added an answer While I agree with nilamo and earl, I hope I… May 12, 2026 at 10:04 am
  • Editorial Team
    Editorial Team added an answer Mirah (formerly Duby) exists, because static typing can result in… May 12, 2026 at 10:04 am

Related Questions

OK, I know what you're thinking, "why write a method you do not want
Ok, I asked a question earlier about Flex and ADO.NET Data Services but didn't
Ok, I have been working with Solaris for a 10+ years, and have never
OK, I know there have already been questions about getting started with TDD ..
Ok, I have a strange exception thrown from my code that's been bothering me

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.