0
votes

Pretty new to ABAP. I've got a class "Truck" ("vrachtwagen") which has a table attribute it_pakket (packages in the truck). I've written a method to write the contents of this table to the screen (DisplayLading). I want to do this for each truck, so I call this method in a for-loop. The program runs without errors, but the output doesn't show up on the screen.

REPORT ZPR412_OO_OEF1.

CLASS CPakket DEFINITION.
    PUBLIC SECTION.
      DATA: Id TYPE I READ-ONLY,
            aantal TYPE I,
            bestemmeling TYPE STRING.
      METHODS: SetID IMPORTING ID TYPE I.
ENDCLASS.

CLASS CPakket IMPLEMENTATION.
  METHOD SetID.
    me->Id = ID.
  ENDMETHOD.
ENDCLASS.

CLASS CVrachtwagen DEFINITION.
  PUBLIC SECTION.
    DATA: Id TYPE I READ-ONLY.
    METHODS: SetID IMPORTING ID TYPE I,
             LaadPakket IMPORTING Pakket TYPE REF TO CPakket,
             LosPakket IMPORTING Pakket TYPE REF TO CPakket,
             DisplayLading.
  PRIVATE SECTION.
    DATA: it_pakket TYPE STANDARD TABLE OF REF TO CPakket,
          pakket TYPE REF TO CPakket.
ENDCLASS.

CLASS CVrachtwagen IMPLEMENTATION.
  METHOD SetID.
    me->Id = ID.
  ENDMETHOD.
  METHOD LaadPakket.
    APPEND Pakket TO it_pakket.
  ENDMETHOD.
  METHOD LosPakket.
  ENDMETHOD.
  METHOD DisplayLading.
    LOOP AT me->it_pakket into pakket.
      WRITE:/ pakket->Id, pakket->aantal, pakket->bestemmeling.
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.

DATA:
  vrachtwagen TYPE REF TO CVrachtwagen,
  it_vrachtwagens TYPE STANDARD TABLE OF REF TO CVrachtwagen,
  pakket TYPE REF TO CPakket,
  it_pakket TYPE STANDARD TABLE OF REF TO CPakket,
  s TYPE string.

start-of-selection.
DO 2 TIMES.
  CREATE OBJECT vrachtwagen.
  CALL METHOD vrachtwagen->SetId EXPORTING Id = sy-index.
  APPEND vrachtwagen TO it_vrachtwagens.
ENDDO.

CREATE OBJECT pakket.
CALL METHOD pakket->SetId EXPORTING Id = 1.
pakket->aantal = 5.
pakket->bestemmeling = 'Bob'.
APPEND pakket TO it_pakket.

CREATE OBJECT pakket.
CALL METHOD pakket->SetId EXPORTING Id = 2.
pakket->aantal = 2.
pakket->bestemmeling = 'Jan'.
APPEND pakket TO it_pakket.

CREATE OBJECT pakket.
CALL METHOD pakket->SetId EXPORTING Id = 3.
pakket->aantal = 1.
pakket->bestemmeling = 'Piet'.
APPEND pakket TO it_pakket.

LOOP AT it_vrachtwagens INTO vrachtwagen.
  CASE sy-index.
    WHEN 1.
      loop at it_pakket into pakket.
        if sy-index lt 3.
          vrachtwagen->LaadPakket( pakket ).
        endif.
      endloop.
    WHEN 2.
      read table it_pakket into pakket index 3.
      vrachtwagen->LaadPakket( pakket ).
  ENDCASE.
ENDLOOP.
LOOP AT it_vrachtwagens INTO vrachtwagen.
  vrachtwagen->DisplayLading( ).
ENDLOOP.

I've written code to fill the trucks with packages and added the trucks to the internal table over which I loop. My guess is that the write method in the CVrachtwagen class implementation can't write to the screen, because the program is still running the loop?

1
Where do you fill it_pakket? - user5653854
@lausek in a different part of the code (didn't include it in my original post to prevent clutter), I create 3 "pakket" objects and append them to the table it_pakket. I was able to print the output of this table using separate Write statements, so the table it_pakket is filled without a doubt. - RobVH
The problem is very likely in a part of the code that you did not provide. Please edit your question to include a MCVE. Besides that: ABAP is not case sensitive, so I would not recommend you accustom yourself to using the CamelCase naming convention but rather stick to the recommended lower_case_with_underscores naming. Also, names in code should be in English. - vwegert
Have you debugged your code? You can easily do that by calling BREAK-POINT somewhere. But it can't be the case that your program isn't printing because it ran a loop. Well, except the loop is endless... - user5653854
Oh and btw the LOOP statement doesn't use sy-index for counting lines. You should check for sy-tabix which isn't quite a good solution either as it will also be modified by READ TABLE - user5653854

1 Answers

4
votes

The system variable SY-INDEX is not set inside a LOOP AT loop, you need to replace it with SY-TABIX inside your loops over it_vrachtwagens and it_pakket.

From SAP's documentation (not too helpful):

sy-index - Loop index. In DO and WHILE loops, contains the number of previous loop passes, including the current pass.

sy-tabix - Row number in the table index of an internal table. Contains the last row accessed using a primary or secondary table index. Is set to 0 when accessed using a hash algorithm.