Question

I am literally stumped on this one...

When running my code, the program just... doesn't run the rest of the code after I attempt to do anything with arrays or lists within in form load event, here's my code:

Public Shared alerts As String()

Private Sub Popup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Width - Me.Width, Screen.PrimaryScreen.WorkingArea.Height - Me.Height)
    ' Check for alerts
    If My.Settings.hasadmin = False Then
        MsgBox("test1")
        pb_alert.Visible = True
        createAlert("Some functions require admin privileges.")
    End If
End Sub

Private Sub createAlert(ByVal msg As String)
    MsgBox("test2")
    updateAlerts()
    MsgBox("test5")
End Sub

Private Sub updateAlerts()
    MsgBox("test3")
    Dim length = alerts.Length
    MsgBox("test4")
End Sub

I don't know why this is happening...

Messageboxes coming up are: "test1" "test2" "test3" then nothing, because I accessed the alerts array?

I have no clue, help please!

ALSO I see no errors or compiling problems or ANYTHING!

The program continues after this, but it just won't bring up the other message boxes, which I want to replace with actual code of course.

Was it helpful?

Solution

The issue is that an exception is being thrown because alerts is Nothing.

The easy way around this is to initialize the string with a -1 size so that it is an empty array.

Public Shared alerts As String(-1)

The correct way is to test for Nothing before attempting to use it.

If alerts IsNot Nothing Then
  ' Do something with it

The other thing that you must do is add exception handling to the load event or add a AppDomain unhandled exception handler or handle the WinforsFormsApplicaBase UnhandledException event.

Load event:

Try
Catch theException As Exception
  Call MsgBox(theException.Message)
End Try
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top