Using the Visual Studio designer - "Object reference not set to an instance of an object" (Visual Studio 2008)

15,868

Solution 1

You have a bug in design mode for some custom control, probably PageScroller, and apparently starting from the constructor. Perhaps there's some code in the constructor that returns null in design mode, and the null is not checked for.

Solution 2

I occasionally see problems like this. I started moving code from the constructor to the load event and that helped.

Solution 3

Make sure that the auto generated

InitializeComponent();

method is called before trying to reference any object created in designer.

Share:
15,868
Kivus
Author by

Kivus

Former Software Engineer in both Charlotte and Raleigh / Durham, now learning the law at UNC.

Updated on June 28, 2022

Comments

  • Kivus
    Kivus almost 2 years

    I recently got tossed some C# code to make some minor cosmetic changes to. When I open up some of the files in Visual Studio though, I get errors saying:

    To prevent possible data loss...

    The first of these errors is:

    Object reference not set to an instance of an object.

    I can follow the stack trace fine, but I'm not sure what I really should be looking for in this situation. Also, the end of my stack trace has a call that ends in "PageScroller..ctor()".

    Based on a little Google research, I'm assuming that means call the constructor. Is that true?

  • Kivus
    Kivus almost 15 years
    Is it common (or at least acceptable) in the C# world to have a number of pages where they work fine but they are not viewable in the designer? It seems most of the page I'm looking at do function, just don't show up in the designer. Overall, that would make it easier for me to fix things so I don't have to learn the designer interface, but I wanted to know if that would be considered "bad practice"
  • John Saunders
    John Saunders almost 15 years
    No, it's not acceptable, at least not to me. It's not necessary for every control to have extensive Designer support, but it should be possible to at least open the page in the designer, set properties, and save the page, and have the properties persist.
  • Kivus
    Kivus almost 15 years
    Excellent. Thank you for the help.
  • C4d
    C4d almost 9 years
    Nice to see im not the only one facing this problem. I also have a returning null in design mode. The most strange thing: I've placed a catch with a MessageBox which is firing inside the EDITOR when I open the tabPage. IN EDITOR! How is this possible without compiling?
  • John Saunders
    John Saunders almost 9 years
    @C4ud3x: what makes you think it didn't compile? Clearly, it did. The world of the designer is the kind of thing that broadens the mind - or that damages the mind.
  • C4d
    C4d almost 9 years
    @JohnSaunders: I havnt pressed F5 :D. Stupid answer maybe, but the way I think. From my understanding the designer shouldnt fire my events. The designer is my working-ground for designing. Not for handling popup message-boxes!?