Forum > Databases

Forced type conversion causes error


my code is to read records from database and last step reports error

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---//kdatabse.pas type  TRestApiCall = class(TObject)  private    { Private declarations }   public    { Public declarations }    RestApiCallId: Integer;    tag:integer;    Method: string;    UrlString: string;        end; function KDatabase.getARecordAtIndexInTableDatabase(index: integer;  tableNameCode: integer):TObject;begin      case tableNameCode of       Const_RestApiCallTable :       begin          if(index=0)then          begin             aDataSource.DataSet.FindFirst;          end;          aRestApiCall.RestApiCallId := aDataSource.DataSet.Fields[0].AsInteger;// .AsString ;          aRestApiCall.tag := aDataSource.DataSet.Fields[1].AsInteger;          aRestApiCall.Method := aDataSource.DataSet.Fields[2].AsString;          aRestApiCall.UrlString := aDataSource.DataSet.Fields[3].AsString;           aDataSource.DataSet.Findnext;          result:=aRestApiCall;         end ;     else    begin      end;    end;  end; //mainform procedure TmainForm.FormCreate(Sender: TObject);var  I,n: integer;  aRestApiCall:TRestApiCall;  b:Tobject;begin   appDelegate := TappDelegate.Create();   appDelegate.db :=kdatabase.Create( );  appDelegate.db.openDatabase('data.dat');   appDelegate.db.executeSQLString('select * from RestApiCall');  n:=appDelegate.db.getCountOfResultArrayDatabase;  b:=appDelegate.db.getARecordAtIndexInTableDatabase(0, Const_RestApiCallTable );//everything is ok  aRestApiCall:= TRestApiCall( b ) ;//this step: Forced type conversion reports error and show as below     
your comment welcome

Do your functions return a result with every possible codepath ?

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

the code worked in Delphi7, zeos lib/vcl/lcl for sqlite

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.


[0] Message Index

Go to full version