I am having a bit of trouble with converting a string (url) into a Uri. Here is the screenshot of the error:
http://www.imgbomb.com/i/d15/3bk3v.png

Here is the code that is giving me trouble:

    Private Sub ComboBox1_Change(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
        Dim myString As String = ComboBox1.Text.ToString()

        Me.WebBrowser1.Navigate(New Uri(myString))
    End Sub

Is there any reason and ways to fix this?

Here is the full code:

Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic

Public Class Form1
    Public SourceCode As String
    Dim sb As New StringBuilder()
    Dim s As String
    Dim StreamWriter
    Dim file As System.IO.FileStream
    Dim myUri As New Uri(Me.ComboBox1.Text)

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        WebBrowser1.Stop()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.GoBack()
    End Sub

    Private Sub ViewSourceToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewSourceToolStripMenuItem.Click
        SourceCode = WebBrowser1.Document.Body.OuterHtml
        Form2.Show()
    End Sub

    Private Async Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ComboBox1.Items.Add(WebBrowser1.Document.Url)

        For Each item As Object In ComboBox1.Items
            s = s & item.ToString & Environment.NewLine
        Next

        Dim mydocpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim sb As StringBuilder = New StringBuilder()

        sb.Append(s)

        Using outfile As StreamWriter = New StreamWriter("history.txt", True)
            Await outfile.WriteAsync(sb.ToString())
        End Using

    End Sub

    Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
        Try
            Dim FILE_NAME As String = "history.txt"
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
                Dim Line As String = ""
                Do While objReader.Peek() <> -1
                    Line = objReader.ReadLine()
                    ComboBox1.Items.Add(Line)
                Loop
            Else
                file = System.IO.File.Create("history.txt")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
    Private Sub ComboBox1_Change(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
        Dim myString As String = ComboBox1.Text.ToString()

        Me.WebBrowser1.Navigate(New Uri(myString))
    End Sub
End Class

Hi Dj,

I know that this type of error can be frustrating, but it is happening for a logical reason.

When you start application, an instance of Form1 is created. During it's creation the form level variables are created first.

The problem is occuring with this statement:

Dim myUri As New Uri(Me.ComboBox1.Text)

At this point in the initialization of the form, ComboBox1 may be declared but does not yet have an instance of the ComboBox class assigned to it.

If you were to right-click on ComboBox1 in this statement and select "Go To Definition", then Form1.Designer.vb file will open and you will be positioned at it's declaration statement at the bottom of the file. If you scroll up to the start of "Private Sub InitializeComponent()" you will see a the statement:

Me.ComboBox1 = New System.Windows.Forms.ComboBox

This statement is where an instance of Combox is assigned to ComboBox1, but this does not occur until InitializeComponent is called by default "Sub New" of the Form class.

So much for the reason for the problem. Now to remedy the problem.

Change: Dim myUri As New Uri(Me.ComboBox1.Text)

To: Dim myUri As Uri

Add a Form load handler:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   If Not String.IsNullOrEmpty(Me.ComboBox1.Text) Then myUri = New Uri(Me.ComboBox1.Text)
End Sub

You may want to add a similar "If" statement in "Sub ComboBox1_Change" to verify that the text is not empty before creating a Uri from it.

PS: I just took another look at your code. I did not see where you were using "myUri" and did not see any other references. So you could just delete it altogether and not add the Form load handler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.