Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi

 

I can't figure out why this is happening. The app throws a nullreferenceexception on Me.Controls.Add(tab(count)).

 

Public tab As Tab()
Public wb As WebBrowser()
Public count As Integer = 0

Public Sub NewTab(ByVal url As String)
   Try
       wb(count) = New WebBrowser
       tab(count) = New Tab

       wb(count).Name = "wb" & count.ToString()
       wb(count).Location = New Point(0, 69)
       wb(count).Size = New Size(903, 517)
       wb(count).Navigate(url)

       tab(count).Name = "tab" & count.ToString()
       If (count > 1) Then tab(count).Size = tab(count - 1).Size
       If (count = 0) Then tab(count).Size = New Size(212, 32)
       If (count > 1) Then tab(count).Location = New Point(tab(count - 1).Location.X + 212, tab(count - 1).Location.Y)
       If (count = 0) Then tab(count).Location = New Point(120, 0)

       Me.Controls.Add(wb(count))
       Me.Controls.Add(tab(count))

       count = count + 1
   Catch ex As Exception
       MessageBox.Show(ex.Message())
   End Try
End Sub

 

tab() is the name of the array of the usercontrols which has just a label and 2 pictureboxes in it.

 

I know what the error means just not sure why it's doing this. Any tips?

 

Thanks

Posted

I recreated the snippet on my side without the WebBrowser and couldn't reproduce the error. What happens if you run the code without the WebBrowser segment?

Where do you dimension the tab() array?

  • Leaders
Posted

orca may be right, in the code you've posted you don't actually create the array. Might I suggest you ditch the array and go with the nifty List(Of T) class. It's a rare case these days that a collection class doesn't make things simpler.

 

There are some other things I might have done differently, as well. These are only suggestions (except for the Exception issue, which is a very strong suggestion). Take em or leave em.

Public tabs As New [color="Blue"]List(Of Tab)[/color]
Public wbs As New [color="Blue"]List(Of WebBrowser)[/color]

Public Sub NewTab(ByVal url As String)
   Try
       [color="Blue"]Dim newWb As New WebBrowser
       Dim newTab As New Tab[/color]

       newWb.Name = "wb" & count.ToString()
       newWb.Location = New Point(0, 69)
       newWb.Size = New Size(903, 517)
       newWb.Navigate(url)

       newTab.Name = "tab" & count.ToString()

       [color="Blue"]Dim IsFirstTab As Boolean = tabs.Count = 0[/color]
       If (IsFirstTab) Then 
           newTab.Size = New Size(212, 32)
           newTab.Location = New Point(120, 0)
       Else
           newTab.Size = tab(count - 1).Size
           newTab.Location = New Point(tab(count - 1).Location.X + 212, tab(count - 1).Location.Y)
       End If

[color="Blue"]        wbs.Add(newWb)
       tabs.Add(newTab)
[/color]        
       Me.Controls.Add(newWb)
       Me.Controls.Add(newTab)
   
   Catch ex As Exception
[color="Blue"]        ' Bad plan, man. You only want to capture exceptions you are prepared to handle.
       ' You should make a point of NOT catching things like OutOfMemoryException.
[/color]        MessageBox.Show(ex.Message())
   End Try
End Sub

[sIGPIC]e[/sIGPIC]
Posted

Thanks for the replies!

 

Funny, I think you may be right. Bit of research and found I am essentially declaring and initializing it at the same time. The array with at least one element in it worked fine (which defeats the purpose of the code).

 

Without the WebBrowser code segment the tab shows up (still throws the exception) but it's at the wrong location. So no go there either.

 

I think the List will have to do it. Seems to do the job so far.

 

And yeah, maybe I will handle exceptions a bit better, just think it's better than having the whole thing crash.

 

Thanks

  • Leaders
Posted
And yeah, maybe I will handle exceptions a bit better, just think it's better than having the whole thing crash.

 

I understand the sentiment, but there is a reason that this is discouraged. The idea that an exception is supposed to convey is "an error condition that you aren't expecting and probably can't handle", such as running out of memory.

 

What you really want is to prevent the conditions that produce an exception. Make sure a file exists before you try to read from it. Sometimes you can't prevent an exception, but you know you can recover from it. For example, I wouldn't expect you to verify an image file is completely valid before creating an Image object from the file, but if the image file is invalid, it would make sense to capture the exception, and probably show the user a message such as "The image file is invalid."

 

As long as you avoid causing exceptions, and handle the ones you can reasonably expect, any other exceptions represent a situation that you aren't prepared for and probably can't recover from. When you handle an exception, you are saying, "I can fix this," and if you blindly handle all exceptions, you're going to cause more harm than good. That's why so many recommend that your program "fail fast" when an error occurs; something is wrong, and the program doesn't know what it is.

 

If you insist on handling the error, at least inform the user that an error occurred, and the program may be in an inconsistent state (i.e. all bets are off).

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...