0

So im working on my gmail application which will allow me to log in to my gmail with ease. And what I have here is a webBrowser control and some HtmlElements to store some data in memory.

When I try to run this (When I press the Connect event) I keep getting this error.

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Now what I think this is, is that its not finding the propper ID even though it is the correct one. but thats why im here im probably wrong. The first two elements works just fine, It typed in the email adress and clicks next but as soon as its going to type into the password field it throws that exception.

private void btnConnect_Click(object sender, EventArgs e)
        {
            //httpwebrequest
            //find elementID
            //invoke the variable to the elementID
            //Elements - EmailID = Email | PasswordID = Passwd

            //

            HtmlDocument doc = webBrowser1.Document;
            HtmlElement usernameElement = doc.GetElementById("Email");
            HtmlElement loginBtnElement = doc.GetElementById("signIn");
            HtmlElement passwordElement = doc.GetElementById("Passwd");
            usernameElement.SetAttribute("value", "currentemailAdress@gmail.com");
            loginBtnElement.InvokeMember("click");
            passwordElement.SetAttribute("value", "password"); //This is the line
            //loginBtnElement.InvokeMember("click");

        }
NerdzIT
  • 237
  • 2
  • 13
  • Did you debug? Did you check that `passwordElement` isn't `null`? Did you check that getting the `value` attribute gets a proper attribute? – Gilad Green Apr 03 '17 at 10:00
  • If you want to get help for this question, you should at least specify on which line (and variable) you are getting this exception. Otherwise, you could try to run your code in the debugger and see what's causing the problem. – Paolo Tedesco Apr 03 '17 at 10:01
  • @PaoloTedesco I commented out the line in which i was getting the error, see the questiona bove. – NerdzIT Apr 03 '17 at 10:01
  • _Is_ there an element in `doc` with the ID of `"Passwd"`? Is that something you have made sure of? Have you stepped through your code and inspected your objects in the debugger? – Abion47 Apr 03 '17 at 10:02
  • @Abion47 I think I can find the solution through that comment I will be back with an answer! – NerdzIT Apr 03 '17 at 10:07
  • Wrong flag by moderator, it is to do with letting the document load. @Christoffer, you need code, type `webBrowser1.DocumentCompleted+=` and then press TAB twice.and C# IDE will generate an event handler for you. Do your processing in that event handler. I wish I could add an answer but flagger has golden hammer privileges. – S Meaden Apr 03 '17 at 10:12
  • I was going to make that an answer when I figured it out but I couldnt because the mod made this a dupe.. Anyways thanks sir! – NerdzIT Apr 03 '17 at 10:15
  • @SMeaden Somewhat odd that it would only fail on the password element, though. I would think if it were an issue of the document having not yet been loaded, it would've failed immediately on the username element. – Abion47 Apr 03 '17 at 10:33

0 Answers0