Recent

Author Topic: "Object reference is nil" error  (Read 7322 times)

facido

  • New Member
  • *
  • Posts: 15
"Object reference is nil" error
« on: July 10, 2018, 06:12:01 am »
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][+][-]
  1. procedure Tmain.GoToURLClick(Sender: TObject);
  2. Var   Browser:TEvsWebBrowser;
  3.       Doc: IHTMLDocument2;
  4.       WebForm: IHTMLFormElement;
  5.       FormElements: OleVariant;
  6.       _data: variant;
  7.       url,onull:Olevariant;
  8.       ActiveXContainer:TActiveXContainer;
  9. begin
  10.  
  11.       Browser:=TEvsWebBrowser.Create(Self);
  12.  
  13.       ActiveXContainer:=TActiveXContainer.Create(Self);
  14.       ActiveXContainer.ComServer:=Browser.ComServer;
  15.       ActiveXContainer.Active:=true;  
  16.  
  17.       url:=Utf8decode('https://myurl.com/login');
  18.       onull:=NULL;
  19.       Browser.ComServer.Navigate2(url,onull,onull,onull,onull);
  20.  
  21.       doc := Browser.ComServer.Document as IHTMLDocument2;  
  22.       _data := 'test';
  23.  
  24.       WebForm := Doc.Forms.Item(0,'') as IHTMLFormElement;   // "Object reference is nil" error occurs here
  25.       FormElements := WebForm.Elements;  
  26.  
  27.  
  28.    .................................................
  29.  

Why am I getting this error?
« Last Edit: July 10, 2018, 06:15:04 am by facido »

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: "Object reference is nil" error
« Reply #1 on: July 10, 2018, 06:40:41 am »
Code: Pascal  [Select][+][-]
  1.       WebForm := Doc.Forms.Item(0,'') as IHTMLFormElement;   // "Object reference is nil" error occurs here
Why am I getting this error?
Doc or Doc.Forms is nil.

facido

  • New Member
  • *
  • Posts: 15
Re: "Object reference is nil" error
« Reply #2 on: July 10, 2018, 07:05:07 am »
How can I fix this?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: "Object reference is nil" error
« Reply #3 on: July 10, 2018, 07:40:24 am »
How 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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: "Object reference is nil" error
« Reply #4 on: July 10, 2018, 07:56:33 am »
@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.html

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: "Object reference is nil" error
« Reply #5 on: July 10, 2018, 09:57:26 am »
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.
« Last Edit: July 10, 2018, 11:26:32 am by Munair »
keep it simple

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: "Object reference is nil" error
« Reply #6 on: July 10, 2018, 10:40:04 am »
That would be even more confusing (and wrong).
I suppose you mean something like:
Code: Pascal  [Select][+][-]
  1. Var Doc: IHTMLDocument2;
  2. begin
  3. doc :=THTMLDocument2.Create as IHtmlDocument;// etc ---
« Last Edit: July 10, 2018, 10:41:39 am by Thaddy »
Specialize a type, not a var.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: "Object reference is nil" error
« Reply #7 on: July 10, 2018, 11:34:41 am »
That would be even more confusing (and wrong).
I suppose you mean something like:
Code: Pascal  [Select][+][-]
  1. Var Doc: IHTMLDocument2;
  2. begin
  3. doc :=THTMLDocument2.Create as IHtmlDocument;// etc ---
"something like" indeed. Feel free to explain classes and interfaces to the OP Thaddy.  :D
keep it simple

facido

  • New Member
  • *
  • Posts: 15
Re: "Object reference is nil" error
« Reply #8 on: July 10, 2018, 12:21:20 pm »
Code: Pascal  [Select][+][-]
  1. Var Doc: IHTMLDocument2;
  2. begin
  3. doc :=IHTMLDocument2.Create as IHtmlDocument;
  4.  

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

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: "Object reference is nil" error
« Reply #9 on: July 10, 2018, 12:30:38 pm »
Code: Pascal  [Select][+][-]
  1. Var Doc: IHTMLDocument2;
  2. begin
  3. doc :=IHTMLDocument2.Create as IHtmlDocument;
  4.  

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;.... <sigh>
You can't call create an interface, you can call create on a class and assign it with "as IHtmlDocument" to  interface variable.
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: "Object reference is nil" error
« Reply #10 on: July 10, 2018, 12:43:25 pm »
"something like" indeed. Feel free to explain classes and interfaces to the OP Thaddy.  :D

OK, basic example on how to use (COM style, proper style ) interfaces:
Code: Pascal  [Select][+][-]
  1. // simple interface example
  2. {$mode delphi}
  3. uses classes;
  4. type
  5.   // this is an interface. it is a contract:
  6.   // if a class is declared with this interface, it promises to have all methods of the interface
  7.   IMyinterface = interface
  8.     ['{DD3BF14A-2DDE-42CE-9ABB-3DFD0CF0C273}']
  9.     procedure testme;
  10.   end;
  11.  
  12.   // This is a class declared with the above interface
  13.   // It has implemented the method of the interface
  14.   TMyinterfacedClass = class(TinterfacedObject,IMyInterface)
  15.   public
  16.     procedure Testme;
  17.   end;
  18.  
  19.   procedure TMyInterfacedClass.TestMe;
  20.   begin
  21.     writeln('tested me');
  22.   end;
  23.  
  24. var
  25.   test:IMyInterface; //Interface NOT class...
  26. begin
  27.   // use the create from the class, but assign to an interface variable
  28.   test := TMyInterfacedClass.Create as IMyInterface; //
  29.   test.testme;
  30.   // no need to free it...is an interface: will be released when out of scope.
  31. end.

Maybe he learns something.... Don't keep your hopes up.
« Last Edit: July 10, 2018, 12:47:43 pm by Thaddy »
Specialize a type, not a var.

facido

  • New Member
  • *
  • Posts: 15
Re: "Object reference is nil" error
« Reply #11 on: July 10, 2018, 02:59:42 pm »
Code: Pascal  [Select][+][-]
  1. Var Doc: IHTMLDocument2;
  2. begin
  3. doc :=IHTMLDocument2.Create as IHtmlDocument;
  4.  

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;.... <sigh>
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

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: "Object reference is nil" error
« Reply #12 on: July 10, 2018, 03:26:49 pm »
As far as I know it does. If you have the appropiate typelibrary..Anyway there's a way around it using late binding.
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: "Object reference is nil" error
« Reply #13 on: July 10, 2018, 09:15:41 pm »
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][+][-]
  1.  doc := Browser.ComServer.Document as IHTMLDocument2;  
  2.  
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.
« Last Edit: July 10, 2018, 09:17:55 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

facido

  • New Member
  • *
  • Posts: 15
Re: "Object reference is nil" error
« Reply #14 on: July 11, 2018, 03:25:55 am »
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
« Last Edit: July 11, 2018, 03:40:10 am by facido »

 

TinyPortal © 2005-2018