0

I'm quite new at programming, and I'm having trouble with an Access Violation in the DrawCell Procedure below.

I'm creating a maze and each cell in the 2D Array maze has a boolean attribute called "wall". When the program starts, all cells in the array are set to be a "Wall"

the access violation occurs at the line

if maze[ACol, ARow].Wall = true then




procedure TfrmMazeGame.StrGridMazeDrawCell(Sender: TObject; ACol, ARow: 
integer; Rect: TRect; State: TGridDrawState);
begin
    if maze[ACol, ARow].Wall = true then
    begin
        StrGridMaze.canvas.Brush.Color := clGreen
    end
    else if maze[ACol, ARow].Wall = false then
        StrGridMaze.canvas.Brush.Color := clblue;
    StrGridMaze.canvas.fillrect(Rect);
end;
  • 4
    What is the value of `ACol` and `ARow`? What is the declaration of `maze`? Time to learn how to debug. – LU RD Feb 16 '19 at 13:09
  • 1
    As a matter of style it is usual to write `if maze[ACol, ARow].Wall then` not `if maze[ACol, ARow].Wall = true then`. Also you don't need the second if statement (on the else line) because maze[ACol, ARow].Wall must be false. – Keith Miller Feb 16 '19 at 14:15
  • 1
    There can only be one reason: some pointer is invalid. In that line, there is only one pointer: maze, or to be more precise: maze[ACol, ARow]. Check if maze was iniitalized with SetLength, if it has the proper dimensions and if ACol and ARow are within the bounds of that array. And indeed: start debugging. That is very easy, in Delphi. Just set a breakpoint and then single step. Watch the values of the variables: is maze perhaps nil, are ARow or ACol perhaps outisde the bounds of the array, etc.. – Rudy Velthuis Feb 16 '19 at 14:28
  • 2
    Go to Projects -> Options -> Building -> Delphi Compiler -> Compiling and set the check marks for "Overflow Checking" and "Range Checking". Then recompile and run the program. You will most likely get a different error now which should give you a better idea what is wrong. – dummzeuch Feb 16 '19 at 17:54

0 Answers0