"Object reference is nil" error lazarus / 61-Beginners ---------------------------------------------------------------------- facido · 2018-07-10 04:12 I am writing a module to auto fill web form, and receiving the following error at line 24: "Object reference is nil" Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Tmain.GoToURLClick(Sender: TObject);Var   Browser:TEvsWebBrowser;    Doc: IHTMLDocument2;      WebForm: IHTMLFormElement;      FormElements: OleVariant;      _data: variant;      url,onull:Olevariant;      ActiveXContainer:TActiveXContainer;begin     Browser:=TEvsWebBrowser.Create(Self);       ActiveXContainer:=TActiveXContainer.Create(Self);      ActiveXContainer.ComServer:=Browser.ComServer;      ActiveXContainer.Active:=true;          url:=Utf8decode('https://myurl.com/login');      onull:=NULL;      Browser.ComServer.Navigate2(url,onull,onull,onull,onull);       doc := Browser.ComServer.Document as IHTMLDocument2;         _data := 'test';       WebForm := Doc.Forms.Item(0,'') as IHTMLFormElement;   // "Object reference is nil" error occurs here      FormElements := WebForm.Elements;       .................................................  Why am I getting this error? ---------------------------------------------------------------------- ASerge · 2018-07-10 04:40 Quote from: facido on July 10, 2018, 06:12:01 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       WebForm := Doc.Forms.Item(0,'') as IHTMLFormElement;   // "Object reference is nil" error occurs hereWhy am I getting this error? Doc or Doc.Forms is nil. ---------------------------------------------------------------------- facido · 2018-07-10 05:05 How can I fix this? ---------------------------------------------------------------------- taazz · 2018-07-10 05:40 Quote from: facido on July 10, 2018, 07:05:07 amHow can I fix this? check which one is nil and find out why. My guess would be there are no forms in the html you downloaded. ---------------------------------------------------------------------- Handoko · 2018-07-10 05:56 @facido Can you provide the compilable source code? So we can help you to test and debug the problem. If you're not willing to publicize the project, you can write a demo project that showing the issue. To send the demo here: Create a new folder and copy all the necessary files to the folder except: the binary, *.bak and the lib folder. Compress the folder and send the zip file to the forum. And please don't start a new thread if it is not necessary. The original thread is here: https://forum.lazarus.freepascal.org/index.php/topic,41822.msg290982.h tml ---------------------------------------------------------------------- munair · 2018-07-10 07:57 This is the same error as in your previous thread. You assign values/addresses to objects that have not been instantiated. You should create the object with something like doc := THTMLDocument2.Create;. You may want to read about classes first: http://wiki.freepascal.org/Class It shows an example that tackles the problem you're running into. ---------------------------------------------------------------------- Thaddy · 2018-07-10 08:40 That would be even more confusing (and wrong). I suppose you mean something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var Doc: IHTMLDocument2;begindoc :=THTMLDocument2.Create as IHtmlDocument;// etc --- ---------------------------------------------------------------------- munair · 2018-07-10 09:34 Quote from: Thaddy on July 10, 2018, 10:40:04 amThat would be even more confusing (and wrong). I suppose you mean something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var Doc: IHTMLDocument2;begindoc :=THTMLDocument2.Create as IHtmlDocument;// etc ---"something like" indeed. Feel free to explain classes and interfaces to the OP Thaddy. ---------------------------------------------------------------------- facido · 2018-07-10 10:21 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var Doc: IHTMLDocument2;begindoc :=IHTMLDocument2.Create as IHtmlDocument;  The recommended code returns the following compilation error message: Error: identifier idents no member "Create" There is no "Create" member for the class. I cannot attach the source files since the zipped file size is around 350k. ---------------------------------------------------------------------- Thaddy · 2018-07-10 10:30 Quote from: facido on July 10, 2018, 12:21:20 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var Doc: IHTMLDocument2;begindoc :=IHTMLDocument2.Create as IHtmlDocument;  The recommended code returns the following compilation error message: Error: identifier idents no member "Create" There is no "Create" member for the class. I cannot attach the source files since the zipped file size is around 350k. Are you blind? is says THTMLDocument2.Create as IHtmlDocument; not IHTMLDocument2.Create as IHtmlDocument;.... You can't call create an interface, you can call create on a class and assign it with "as IHtmlDocument" to  interface variable. ---------------------------------------------------------------------- Thaddy · 2018-07-10 10:43 Quote from: Munair on July 10, 2018, 11:34:41 am"something like" indeed. Feel free to explain classes and interfaces to the OP Thaddy.  OK, basic example on how to use (COM style, proper style ) interfaces: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // simple interface example{$mode delphi}uses classes;type  // this is an interface. it is a contract:  // if a class is declared with this interface, it promises to have all methods of the interface  IMyinterface = interface    ['{DD3BF14A-2DDE-42CE-9ABB-3DFD0CF0C273}']    procedure testme;  end;    // This is a class declared with the above interface  // It has implemented the method of the interface  TMyinterfacedClass = class(TinterfacedObject,IMyInterface)  public    procedure Testme;  end;    procedure TMyInterfacedClass.TestMe;  begin    writeln('tested me');  end; var  test:IMyInterface; //Interface NOT class...begin  // use the create from the class, but assign to an interface variable  test := TMyInterfacedClass.Create as IMyInterface; //  test.testme;  // no need to free it...is an interface: will be released when out of scope.end. Maybe he learns something.... Don't keep your hopes up. ---------------------------------------------------------------------- facido · 2018-07-10 12:59 Quote from: Thaddy on July 10, 2018, 12:30:38 pmQuote from: facido on July 10, 2018, 12:21:20 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var Doc: IHTMLDocument2;begindoc :=IHTMLDocument2.Create as IHtmlDocument;  The recommended code returns the following compilation error message: Error: identifier idents no member "Create" There is no "Create" member for the class. I cannot attach the source files since the zipped file size is around 350k. Are you blind? is says THTMLDocument2.Create as IHtmlDocument; not IHTMLDocument2.Create as IHtmlDocument;.... You can't call create an interface, you can call create on a class and assign it with "as IHtmlDocument" to  interface variable. I see. However, the problem is THTMLDocument2 does not exist. ---------------------------------------------------------------------- Thaddy · 2018-07-10 13:26 As far as I know it does. If you have the appropiate typelibrary..Anyway there's a way around it using late binding. ---------------------------------------------------------------------- taazz · 2018-07-10 19:15 that has gone long enough. Please try to read the code before answering any questions about the code posted which clearly says. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  doc := Browser.ComServer.Document as IHTMLDocument2;    that line should be clear to any one, that the application is not, and it should never be, the creator of the document, the browser activex control is though. To OP, please upload the zip file somewhere else like google drive or some other free file service and post a link here if you do not want to get involved with the silliness of google, microsoft and all their "security processes" use one of the less used versioning sites (github, gitlab etc) to create a project and upload your zip there. ---------------------------------------------------------------------- facido · 2018-07-11 01:25 Sample codes for troubleshooting have been zipped and uploaded into GitHub. The url to download is as follows: https://github.com/facido/Demo/raw/master/Demo.zip ---------------------------------------------------------------------- ASerge · 2018-07-11 08:00 Quote from: facido on July 11, 2018, 03:25:55 amhttps://github.com/facido/Demo/raw/master/Demo.zip As said before, Doc is nil. You just do not wait for the page to load, and immediately rush to use it. I suspect that many other necessary properties are not established. I propose to look for a ready-made working example before doing it yourself. Or refer to the documentation.