1.11M Members

Object Instances Throwing Errors

 
0
 

Hello everyone, I am working on a VB project which implements a number of custom controls. So far I have had no issue with the controls I have been writing; until now. Every time I add the control to the form it appears like a normal control, however when I run the application, it throws a very vague "An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object" error.

This is the code for my object (I don't think you need to read through all of it):

Public Class RBDSOutput
    Public Const SCROLL_CUT As Integer = 0
    Public Const SCROLL_WORD As Integer = 1
    Public Const SCROLL_CONSTANT As Integer = 2

    Private CurrentData As String
    Private CurrentScrollValue As Integer
    Private CurrentScrollText As String
    Private CurrentScrollWaiting As Integer

    Private OutputComm As String
    Private OutputIP As String
    Private OutputPort As String
    Private OutputFile As String

    Private OutputMode As Integer

    Private MAX_CHARS As Integer

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
#Region "Properties"
    Public Overrides Property Text As String
        Get
            Return Lbl_Caption.Text
        End Get
        Set(value As String)
            Lbl_Caption.Text = value
            Lbl_OutputDisplay.Left = Lbl_Caption.Right + 2

            Me.Width = Lbl_OutputDisplay.Right + 2
        End Set
    End Property

    Public Property Data As String
        Get
            Return CurrentData
        End Get
        Set(value As String)
            CurrentData = value
            Call SendText(CurrentData, True)
        End Set
    End Property

    Public Property MaximumCharacters As Integer
        Get
            Return MAX_CHARS
        End Get
        Set(value As Integer)
            MAX_CHARS = value
            ResizeOutputToCharacter(MAX_CHARS)
        End Set
    End Property

    Public Property ScrollMode As Integer
#End Region
    Private Sub ResizeOutputToCharacter(ByVal chars As Integer)
        Lbl_Caption.Width = Convert.ToInt32(chars * 0.625)
        Invalidate()
    End Sub
#Region "Text Output"
    Private Delegate Sub SendTextDelegate(ByVal text As String, ByVal scroll As Boolean, ByVal maxLength As Integer)
    'Text to be displayed on the radio is sent to this subroutine
    'text = Text to be dislayed
    'scroll = Will the text be displayed as crolling text (chopped off otherwise)?
    'maxLength = The maximum length of characters to be displayed at one time
    Private Sub SendText(ByVal text As String, ByVal scroll As Boolean)
        Dim tmpText As String

        tmpText = text.Trim
        If tmpText.Length > MAX_CHARS Then
            If scroll Then
                CurrentScrollText = text
                CurrentScrollValue = 0
                CurrentScrollWaiting = 5
                Lbl_OutputDisplay.Text = CurrentScrollText.Substring(0, MAX_CHARS)
                Tmr_TextScroll.Start()
            Else

            End If
        Else
            Lbl_OutputDisplay.Text = text
        End If
    End Sub
    Private Sub Tmr_TextScroll_Tick(sender As Object, e As EventArgs) Handles Tmr_TextScroll.Tick
        Dim textChunk As String

        If CurrentScrollWaiting > 0 Then
            CurrentScrollWaiting -= 1
            Exit Sub
        End If

        If CurrentScrollText.Length - (CurrentScrollValue) < MAX_CHARS Then
            Tmr_TextScroll.Stop()
            textChunk = CurrentScrollText
        Else
            textChunk = CurrentScrollText.Substring(CurrentScrollValue, MAX_CHARS)
            CurrentScrollValue += 1
        End If

        SendText(textChunk, True)
    End Sub

    Public Sub Clear()
        CurrentScrollText = ""
        CurrentData = ""
    End Sub
#End Region
End Class

I am guessing there is an issue somewhere where variables are not being created as new instances of themselves. Any help is greatly appreciated. Thanks!

 
0
 

Your problem might be with these:

Private CurrentData As String
Private CurrentScrollValue As Integer
Private CurrentScrollText As String
Private CurrentScrollWaiting As Integer
Private OutputComm As String
Private OutputIP As String
Private OutputPort As String
Private OutputFile As String
Private OutputMode As Integer
Private MAX_CHARS As Integer

Either intialize them in the New constructor or give them default values in the declaration

Private CurrentData As String = ""
Private CurrentScrollValue As Integer = 0
Private CurrentScrollText As String = ""
Private CurrentScrollWaiting As Integer = 0
Private OutputComm As String = ""
Private OutputIP As String = ""
Private OutputPort As String = ""
Private OutputFile As String = ""
Private OutputMode As Integer = 0
Private MAX_CHARS As Integer = 0

On a side note, when you have a number of related constants:

Public Const SCROLL_CUT As Integer = 0
Public Const SCROLL_WORD As Integer = 1
Public Const SCROLL_CONSTANT As Integer = 2

If you make them an enum instead, you can pass the value by the name but you can only use those values.

 
0
 

Thanks! Initializing the variables seems to have fixed the issue. I am assuming, as a rule of thumb, that all instance variable should be initialized in order to avoid the aforementioned error? Also, thanks for the tip about enum's, I will look into using them.

 
0
 

There some types you can get away without initializing. I find it easier to just initialize everything as a good practice.

You're very welcome. Please remember to mark this solved. Thank you.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: