Recent

Author Topic: Forced type conversion causes error  (Read 6397 times)

teriyaki

  • New Member
  • *
  • Posts: 47
Forced type conversion causes error
« on: December 06, 2021, 03:18:13 pm »
my code is to read records from database and last step reports error

Code: Pascal  [Select][+][-]
  1. //kdatabse.pas
  2.  
  3. type
  4.   TRestApiCall = class(TObject)
  5.   private
  6.     { Private declarations }
  7.  
  8.   public
  9.     { Public declarations }
  10.     RestApiCallId: Integer;
  11.     tag:integer;
  12.     Method: string;
  13.     UrlString: string;
  14.      
  15.  
  16.   end;
  17.  
  18. function KDatabase.getARecordAtIndexInTableDatabase(index: integer;
  19.   tableNameCode: integer):TObject;
  20. begin
  21.  
  22.  
  23.     case tableNameCode of
  24.        Const_RestApiCallTable :
  25.        begin
  26.           if(index=0)then
  27.           begin
  28.              aDataSource.DataSet.FindFirst;
  29.           end;
  30.           aRestApiCall.RestApiCallId := aDataSource.DataSet.Fields[0].AsInteger;// .AsString ;
  31.           aRestApiCall.tag := aDataSource.DataSet.Fields[1].AsInteger;
  32.           aRestApiCall.Method := aDataSource.DataSet.Fields[2].AsString;
  33.           aRestApiCall.UrlString := aDataSource.DataSet.Fields[3].AsString;
  34.  
  35.           aDataSource.DataSet.Findnext;
  36.           result:=aRestApiCall;
  37.  
  38.         end ;
  39.  
  40.     else
  41.     begin
  42.  
  43.  
  44.     end;
  45.     end;
  46.  
  47.  
  48. end;
  49.  
  50. //mainform
  51.  
  52. procedure TmainForm.FormCreate(Sender: TObject);
  53. var
  54.   I,n: integer;
  55.   aRestApiCall:TRestApiCall;
  56.   b:Tobject;
  57. begin
  58.  
  59.   appDelegate := TappDelegate.Create();
  60.   appDelegate.db :=kdatabase.Create( );
  61.   appDelegate.db.openDatabase('data.dat');
  62.  
  63.   appDelegate.db.executeSQLString('select * from RestApiCall');
  64.   n:=appDelegate.db.getCountOfResultArrayDatabase;
  65.   b:=appDelegate.db.getARecordAtIndexInTableDatabase(0, Const_RestApiCallTable );//everything is ok
  66.   aRestApiCall:= TRestApiCall( b ) ;//this step: Forced type conversion reports error and show as below
  67.  
  68.  
  69.    

your comment welcome


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Forced type conversion causes error
« Reply #1 on: December 06, 2021, 03:23:51 pm »
Do your functions return a result with every possible codepath ?

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Forced type conversion causes error
« Reply #2 on: December 06, 2021, 04:18:30 pm »
Are you counting from zero or from 1? You can decide... It is not a forced type conversion a.k.a. hard-cast.
This is likely an over-index issue
« Last Edit: December 06, 2021, 07:41:31 pm by Thaddy »
Specialize a type, not a var.

teriyaki

  • New Member
  • *
  • Posts: 47
Re: Forced type conversion causes error
« Reply #3 on: December 07, 2021, 02:04:35 am »
the code worked in Delphi7, zeos lib/vcl/lcl for sqlite

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: Forced type conversion causes error
« Reply #4 on: December 07, 2021, 08:36:36 am »
This error (the error message) is an artefact from long ago, when gdb could not fetch real exceptions.... But that aside, its an error none the less.

That aside, exception or other error, how did you narrow it down to that line? Did it run until the breakpoint? Or did the debugger highlight that line?

I believe, in this case it actually happened one line above the read line. The debugger (at least the gdb based one) can sometimes highlight the next line.
That is because it only has the address to which the call will return, and that can be the next line.

There is a stack window (menu: view > debug windows), which may show you more info.

 

TinyPortal © 2005-2018