-2

I have watched an youtube tutorial:- https://www.youtube.com/watch?v=nLfzH4xOVqo&t=197s and tried to use some lines of code which worked for him but not for me. I have been searching for the error but didn't found a working solution. Following is the code for my C# form.

There is a panel on the left side which contains buttons and each button is assigned to specific user control designed layout. You can see the video and get an idea of what I really want. All I want is when user click on any button then a certain User Control Form will open without closing the current from i.e. it will send to front of the current user control after clicking its assigned button.

ERROR IMAGE:- https://drive.google.com/open?id=17ERTUexQ79noWiwZyPGD9u8_7MNj-Cdo

ERROR MESSAGE:- An unhandled exception of type 'System.NullReferenceException' occurred in Software.exe

karan ugale
  • 123
  • 9
  • Edit your code to add a comment showing exactly which line it occurs on. If you don’t know, run the app in drug mode, cause the exception and send us a screenshot of the code panel complete with yellow bar highlight and red X icon – Caius Jard Jun 09 '19 at 03:20
  • I've added a google drive link, you can check it out. Thanks – karan ugale Jun 09 '19 at 03:26

1 Answers1

1

You cannot put any code that access the things on your form before the InitializeComponent() method call, because InitializeComponent is the method that creates all the controls and sets them up. Trying to access them before they have been created will result in an null reference exception

InitializeComponent(); should be the first line in your constructor. Move the code around so that this is the case

I’m also not really sure why you’re setting the heights of the panels and buttons in code; just click on them in the visual designer and set their Height property in the properties grid. It’ll either be as a property in its own cell or you’ll find it as a sub-property of the Size property

Caius Jard
  • 72,509
  • 5
  • 49
  • 80