I
I
Ivanq2015-10-28 19:59:06
Programming
Ivanq, 2015-10-28 19:59:06

Nested loops in Pascal?

Hello! I am writing a small program in Pascal (Pascal ABC):

uses GraphABC, Arrays;
var map:array[,] of integer;
var xsize, ysize, x, y:integer;
begin
  ...
  for x := 0 to xsize-1 do
    for y := 0 to ysize-1 do
      map[x, y] := 1;
  ...
end.

Throws an error on the line for y := 0 to ysize-1 do:
Runtime Error: Object reference does not point to an instance of an object.
Looks like something with inner loops. xsize and ysize are defined normally, x and y are also integers. Who faced a similar problem? I did not find it in the search engine.
UPD I also found it - if you remove the array filling, the error is removed, but what to do is not clear.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
buksttabu, 2015-10-28
@Ivanq

your array is dynamic. Try allocating memory for it first: setLength(map, xsize, ysize)

A
AVKor, 2015-10-28
@AVKor

I don't know anything about PascalABC, but in FreePascal, for example, the code is:

const
  xsize = 2;
  ysize = 2;
var
  map : array[0..xsize,0..ysize] of integer;
  x, y : integer;
begin
  for x := 0 to xsize-1 do
    for y := 0 to ysize-1 do
      map[x,y] := 1
end.

will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question