Results 1 to 8 of 8

Thread: [RESOLVED] Error in code. Need help.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Resolved [RESOLVED] Error in code. Need help.

    Hello. I've made a program. The program does what it's supposed to do, for a while. The program is ment to recover my password if i loose it, but when I test it, it failes. It works fine until it comes to the number of my password.

    Then this error shows: object reference not set to an instance of an object. and this section of code is highlighted:
    HTML Code:
    WebBrowser1.Document.GetElementById("ctl03$Login$username$input").SetAttribute("value", TextBox1.Text)
    Here is my completed code:
    HTML Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dialog1.ShowDialog()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If TextBox1.Text = "" Then
                MsgBox("Fyll inn feltene")
                Exit Sub
            ElseIf TextBox2.Text = "" Then
                MsgBox("Fyll inn feltene")
                Exit Sub
            ElseIf ComboBox2.Text = "" Then
                MsgBox("Anngi skole")
                Exit Sub
            ElseIf ComboBox1.Text = "" Then
                MsgBox("Anngi tal for andre del av passordet")
                Exit Sub
            ElseIf TextBox1.Text = "koug" Then
                MsgBox("Administrator har sperret dette fra dette programmet.")
                Exit Sub
            ElseIf TextBox1.Text = "mobe" Then
                MsgBox("Administrator har sperret dette fra dette programmet.")
                Exit Sub
            ElseIf TextBox1.Text = "wiro" Then
                MsgBox("Administrator har sperret dette fra dette programmet.")
                Exit Sub
            ElseIf TextBox1.Text = "toso" Then
                MsgBox("Administrator har sperret dette fra dette programmet.")
                Exit Sub
            ElseIf TextBox1.Text = "laor" Then
                MsgBox("Administrator har sperret dette fra dette programmet.")
                Exit Sub
            End If
    
            MsgBox("Ser etter passordet. Du vil bli varsla når programmet er ferdig.")
    
            info()
    
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'open website in wb
            Number1.Text = "0"
            number2.Text = "0"
            number3.Text = "0"
    
            WebBrowser1.Navigate("https://www.itslearning.com/Index.aspx?CustomerId=565&Username=")
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
            'select combobox item in webpage
            If ComboBox2.SelectedIndex <> -1 Then
                Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl03_Login_site_input")
                Dim optionsElement As HtmlElement = element.GetElementsByTagName("option").Cast(Of HtmlElement).First(Function(el) el.InnerText = ComboBox2.Text)
                optionsElement.SetAttribute("selected", "selected")
            End If
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            'fill combobox
            If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then Return
            Dim element As HtmlElement = WebBrowser1.Document.GetElementById("ctl03_Login_site_input")
            Dim options() As String = element.GetElementsByTagName("option").Cast(Of HtmlElement).Select(Function(el) el.InnerText).ToArray
            ComboBox2.Items.AddRange(options)
        End Sub
    
        Private Sub info()
            Timer1.Stop()
            Number1.Text = Number1.Text + 1
            If Number1.Text = "10" Then
                Number1.Text = "0"
                number2.Text = number2.Text + 1
            ElseIf number2.Text = "10" Then
                number2.Text = "0"
                number3.Text = number3.Text + 1
            ElseIf number3.Text = "10" Then
                number3.Text = "9"
            ElseIf number3.Text = "9" & number2.Text = "9" & Number1.Text = "9" Then
                MsgBox("Fant ikke passordet")
            End If
    
            Dim cur1 As String
            cur1 = number3.Text & number2.Text & Number1.Text
            Label6.Text = cur1
    
            WebBrowser1.Document.GetElementById("ctl03$Login$username$input").SetAttribute("value", TextBox1.Text)
            WebBrowser1.Document.GetElementById("ctl03$Login$password$input").SetAttribute("value", TextBox2.Text & cur1)
            WebBrowser1.Document.GetElementById("ctl03$Login$loginbtn").InvokeMember("click")
    
            Timer2.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            info()
        End Sub
    
        Private Sub info2()
            Timer2.Stop()
    
            Dim cur2 As String
            cur2 = number3.Text & number2.Text & Number1.Text
    
            If WebBrowser1.Url.ToString = "https://www.itslearning.com/main.aspx?StartUrl=main%2fmainmenu.aspx" Then
                MsgBox("Passordet ble funnet:  " & TextBox2.Text & cur2)
                Exit Sub
            Else
                Timer1.Start()
            End If
    
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            info2()
        End Sub
    End Class
    Can someone a bit more experienced please tell me what's wrong? Thanks in advance :-)

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Error in code. Need help.

    Imagine you are doing this:

    House.Room.Cupboard.Box.Beans.Name = "Heinz"

    In that above fake line of code, I am asking for a tin of beans, inside a box, inside a cupboard, inside a room, inside a house.

    What would happen, if you did not have a box in the cupboard? What would happen if you did not even have a cupboard?

    That is your problem, one of the items in the line is missing, it could be:

    WebBrowser1
    or
    WebBrowser1.Document
    or
    WebBrowser1.Document.GetElementById("ctl03$Login$username$input")

    You need to make sure you have something at each level.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Error in code. Need help.

    Don't understand 100&#37; what you mean. But I get the idea. It's like if you want the content of a numeric counter to be converted to string right? and instead of typing: NumericUpDown1.value.toString you type NumericUpDown1.toString right?

    So I think i understand what you mean, but I don't understand what that got to do with the highlighted line:
    Code:
    WebBrowser1.Document.GetElementById("ctl03$Login$username$input").SetAttribute("value", TextBox1.Text)
    Can you please tell me exactly whats wrong with the code and/or missing in it? Thanks

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Error in code. Need help.

    That isn't what he means. In your line, you have these objects:

    1) WebBrowser
    2) WebBrowser1.Document
    3) WebBrowser1.Document.GetElementById("ctl03$Login$username$input")
    4) WebBrowser1.Document.GetElementById("ctl03$Login$username$input").SetAttribute("value", TextBox1.Text)
    5) Textbox1

    One of those objects is Nothing. The first one is virtually impossible, as is the last one. I also think that #4 can't be the problem. That leaves two, though. When the error occcurs, highlight each one in turn and press Shift+F9. One of them is Nothing. Which one?

    Once you know which one is the problem, you are well on your way to solving the problem, but without that, we can't suggest much of anything.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Error in code. Need help.

    WebBrowser1.Document.GetElementById("ctl03$Login$username$input")
    shows up as "nothing" while
    WebBrowser1.Document
    shows up as "{System.Windows.Forms.HtmlDocument}"

    The following two lines of code also shows up as "nothing" but when i just highlight "WebBrowser1.Document" they show up as something.
    Code:
    WebBrowser1.Document.GetElementById("ctl03$Login$password$input")
    and
    Code:
    WebBrowser1.Document.GetElementById("ctl03$Login$loginbtn")
    Now, what am I supposed to do about it? Thank you for all your replies so far.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Error in code. Need help.

    You have now narrowed the problem down, but I'm not sure that I can say much more about it. What you have discovered is that there is no element in the document with that ID. It's quite an id, though, so it could be as simple as a typo. Otherwise, perhaps that isn't the ID of what you were looking for, or perhaps that document isn't the document you expected. There isn't a general answer, at this point. You know what the problem is, you just don't know why, and it may be that nobody else can know any better.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    103

    Re: Error in code. Need help.

    Thank you for all your replies. But still, I can't solve the problem. I just don't know what to do. :S

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Error in code. Need help.

    WebBrowser1.Document.GetElementById("ctl03$Login$password$input")

    What possible reasons do you think you would get nothing when searching for an element? How about the obvious one of the element not actually existing? You can view the whole document text to confirm this, the version that is picked up by your WebBrowser.Document NOT the one you might see with IE. The reason is simple, often the names are random, first time it might be ctl03, next time might be ctl05 or ctl10101001010, you need to confirm that one page you are looking at contains what you think it contains.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width