1

I'm having this InvalidOperationException

An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

pointing at this line of code:

Private Sub BeginningBalancesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeginningBalancesToolStripMenuItem.Click
        inventory.MdiParent = Me <--error here
        inventory.Show()
    End Sub

EDIT: Im only using drag and drop for creating controls from the toolbox. The thing here is, everything works fine until I relocated AND decided to return panel_001 to its previous location by pressing ctrl-z in the inventory.vb during design time to revert the change I made. The program runs fine without error prior to that specific panel relocation. I never edited any codes in the designer.vb and in the inventory class. I strongly believe that a piece of code wasn't properly restored at the ctrl-z action in the inventory.Designer.vb. Is this a visual studio bug?

EDIT: I tried creating a new winform and attempted to start fresh by foolishly copying & pasting all the controls and the forms' class and was no good. I assume the problem does not lie under the striked-out line above.

Adrian
  • 93
  • 1
  • 9
  • possible duplicate of http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Matt Wilko Jun 09 '15 at 12:57
  • If it's a bug, then it's a bug that's about 7 years old... Try with Visual Studio 2013 Community Edition (free) and see if it still happens. – John Saunders Jun 09 '15 at 12:58

2 Answers2

3

The Winforms designer has a knack for tripping really mystifying design-time exceptions. A side-effect from its strong WYSIWYG design, it will run some event handlers at design-time. Like Paint, making a control look the way it does at runtime. Nice, but that does come with a price. This can easily cause an exception if such an event handler was not written to operate correctly at design time. You are supposed to use the DesignMode property to keep the code safe.

Coming up with a way a Click event handler could run at design-time however requires massive amounts of imagination. That is an event that will not run at design time, the designer uses a layered window that intercepts any mouse clicks, using them for design use instead. Like selecting a control or displaying the design-time context menu.

I have actually seen the Click event of a ToolStripMenuItem run. The designer is not 100% watertight but it happened just once and I was hacking the code pretty hard. Coming up with a way that it could possibly run by using Undo is going to be difficult. Maybe you give it the Ctrl+Z shortcut, don't assume that guess is credible.

The way to deal with incomprehensible black magic like this is to just dismiss it and move on with your life. You just don't stand much of a chance to diagnose it and if you do then there's nothing you can do about it anyway because this isn't your code. Well, other than the need to use DesignMode, that may well be necessary. Not in this case. The only thing you have to watch out for is that such an exception did not destroy the InitializeComponent() method. That can happen too, you notice by controls being missing when you re-open the designer. Very unpleasant, you do need a good backup copy in source control to recover from that lossage.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • InitializeComponents actually did some codes went missing `Me.components = New System.ComponentModel.Container Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(inventory))` however, did not fully solved the problem so ok. moving on – Adrian Jun 09 '15 at 15:03
1

OK! after giving it a few more tries, I found the answer. After the exception shows, copy exception details to clipboard and then I pasted it on Notepad. It had a lot of texts but the bottom part was the important one:

--insert wall of texts here-- in C:\Users\Ellen\Documents\Visual Studio 2008\Projects\SampleApplication\SampleApplication\Forms\inventory.Designer.vb:line 760 at CabuyaoWaterDist.inventory..ctor() InnerException:

It pointed me out to the specific line in the designer where the ctrl-z did not properly revert one of the label's caption

Adrian
  • 93
  • 1
  • 9