Session or device error occurred in file AS400/IBM i

I am learning IBM i RPG programming and am stuck on displaying a subfile. I keep getting the error "Session or device error occurred in file". That is supposed to happen in scenarios such as trying to write more than 9999 records to the subfile and turning on SFLDSP indicator on empty subfile but my program has none of such problems. Can anyone help me to spot what I'm doing wrong here?

Display file - STUDD

A                                      DSPSIZ(24 80 *DS3)    
A          R SFLREC                    SFL                     
A            OPT$01         1Y 0B  6 10EDTCDE(Z)               
A            ID$            3Y 0B  6 25EDTCDE(3)               
A            NAME$         20   O  6 33                 
A          R SFLCTL                    SFLCTL(SFLREC)          
A                                      CF03(03 'EXIT')         
A                                      CF06(06 'ADD')          
A  30                                  SFLDSP                  
A  31                                  SFLDSPCTL               
A  32                                  SFLCLR                  
A  33                                  SFLEND(*MORE)           
A                                      SFLSIZ(0020)            
A                                      SFLPAG(0005)            
A                                  5 18'STUDENT ID'                        
A                                  5 33'NAME'                              
A                                  5  9'OPT'    

RPGLE source file - STUDR

FSTUDP     IF   E           K DISK                         
D RRN             S              4  0                      
  //Clear subfile                                          
  *in32 = *on;      //SFLCLR                                         
  *in30 = *off;     //SFLDSP                                         
  write sflctl;                                            
  *in32 = *off;     //SFLCLR  
  *in31 = *on;      //SFLDSPCTL                                       
  rrn = 0;                                                   
  //Add one dummy record to subfile and display                                                  
  rrn += 1;                                                   
  id$ = 123;                                                   
  name$ = 'aaa';                                             
  write sflrec;     //**The said exception happens right here.                    
  *in30 = *on;                                               
  exfmt sflrec;                 
  *inlr = *on;                                             


  • The SFILE keyword refers to the subfile control, not the subfile. The error says the RRN is not valid because it does jot receive one

    FSTUDD     CF   E             WORKSTN SFILE(SFLREC:RRN)    

    when change the subfile can't be read/exfmt you have to use the control format there

      exfmt sflctl;