Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm trying to use this chromium webbrowser and enter my textbox.text to one of the textbox inside the website. I'm having trouble on doing that as it always display
Object reference not set to an instance of an object.'

I'm sure I got the 'id' correctly. For example the id of the username box of Facebook.

What I have tried:

Here's my code so far

Imports CefSharp.WinForms
Imports CefSharp
Imports CefSharp.Web
Imports CefSharp.JavascriptBinding
Imports CefSharp.Event

Public Class Form2

    Private WithEvents browser As ChromiumWebBrowser

    Sub New()
         InitializeComponent()
        InitializeChromium() 
    End Sub

    Private Sub InitializeChromium()
        Dim settings As New CefSettings()
        CefSharp.Cef.Initialize(settings)
        Dim browser As New ChromiumWebBrowser("https://www.facebook.com")
        Panel1.Controls.Add(browser)
        browser.Dock = DockStyle.Fill 
    End Sub
 

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew@gmail.com'")


    End Sub 
Posted
Updated 21-Nov-20 17:40pm
Comments
Richard MacCutchan 15-Nov-20 5:42am    
Which line causes the error?
Member 14993280 15-Nov-20 7:19am    
Hi,
This line

browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew@gmail.com'")

I think i got the wrong code as it doesn't find the if of the textbox on Facebook for email OR that code is incorrect on getting the ID and inputting a value in it.

PS sorry for the previews post here. I'm new here and searching for the correct code haha
Richard MacCutchan 15-Nov-20 7:44am    
Well the message suggests that the getElementById call did not find a field named "email".
Member 14993280 15-Nov-20 7:48am    
Yeah, I've tried looking for the correct code getElementById, getElementByName, getElementByClass
none of those work. unless the code inside this ExecuteScriptAsync is not correct.

Maybe I need to input other than "document.getElementById('email').value='ansdew@gmail.com" ?
Richard MacCutchan 15-Nov-20 7:53am    
The call getElementById is correct, but you need some way of testing that it actually finds the reference before you try to use it. Do not assume that system, or other library, calls will always succeed.

Quote:
VB.NET
Private WithEvents browser As ChromiumWebBrowser
...
Private Sub InitializeChromium()
    ...
    Dim browser As New ChromiumWebBrowser("https://www.facebook.com")
    Panel1.Controls.Add(browser)
    browser.Dock = DockStyle.Fill 
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
    browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew@gmail.com'")
End Sub 
In your InitializeChromium method, you create and initialize a local variable called browser, which hides the browser field.

When your Button1_Click event fires, the browser field is Nothing. You try to call a method on Nothing, and your code fails with a NRE.

Change your initialization method to initialize the field instead of a local variable:
VB.NET
Private Sub InitializeChromium()
    Dim settings As New CefSettings()
    CefSharp.Cef.Initialize(settings)
    browser = New ChromiumWebBrowser("https://www.facebook.com")
    Panel1.Controls.Add(browser)
    browser.Dock = DockStyle.Fill 
End Sub
 
Share this answer
 
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 14993280 15-Nov-20 6:46am    
Hi OriginalGriff,

Thanks for the explanation. I appreciate it.
The line where " Object reference not set to an instance of an object." is pointing to is

browser.ExecuteScriptAsync("document.getElementById('email').value='ansdew@gmail.com'")

This is Visual Basic by the way.
OriginalGriff 15-Nov-20 6:53am    
So us eteh debugger to find out which part of that is null (or Nothing in VB) and you can then start working on why. But until you do, you can't work out anything ...
Member 14993280 15-Nov-20 7:09am    
I think there's nothing null in my stuff. What I think that I need is the correct code/script in order to call the id('email') of the facebook site so I can enter a value.
OriginalGriff 15-Nov-20 7:22am    
You know there is. Because if there wasn't, you wouldn't get "Object reference not set to an instance of an object."

Member 14993280 15-Nov-20 7:46am    
Well, if there is I think it's browser.ExecuteScriptAsync(...) and the code inside it. It doesn't get or find the id of the text of Facebook which is why it tells that object reference set to an instance of an object.

It's the code inside that I'm looking for.
All your answers to my query are very much appreciated.
The only code that makes the
browser
empty is because there's a
Dim
on declaring the
browser
Thanks anyways!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900