System.NullReferenceException in Form Load

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I start the debug of my application, sometimes I get a System.NullReferenceException on the principal Form Load in this portion of code

tlbFixed.Width = frmSSDas.ActiveForm.Size.Widt

What I'm trying to do it's to make the width of the toolbar to ocupie all the width of the form. This error doesn't occur very often.

Considerations
- tlbFixed is a System.Windows.Forms.ToolBa
- frmSSDas is a System.Windows.Form

Private Sub frmSSDas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
tlbFixed.Width = frmSSDas.ActiveForm.Size.Widt
tlbFixed.Refresh(
End Sub '(this is the code of the procedure where it occus - The Language is VB.Net

Thanks in advanc

Rui Pedro Nascimento
 
Hi Rui Pedro,

I am guessing that your ActiveForm may not be defined at certain times, in which case trying to access its Size property will cause a NullReferenceException.

If you use Visual Studio.Net break, when you get the exception break and use the command window (in immediate mode, ctrl-alt-i) to study the properties.

Happy coding!
Morten Wennevik [C# MVP]
 
* "=?Utf-8?B?UnVpIFBlZHJvIE5hc2NpbWVudG8=?= said:
When I start the debug of my application, sometimes I get a System.NullReferenceException on the principal Form Load in this portion of code:

tlbFixed.Width = frmSSDas.ActiveForm.Size.Width

Dud you check if 'ActiveForm' is pointing to a form?

\\\
If Not Form.ActiveForm Is Nothing Then
...
End If
///
 
Back
Top