static method

I am working with winforms and in the file section I have a file that needs to be accessed from a static method.  I get the following error message on fields that are associated with that file...error code COBCH0969, Cannot access object data from a static method or field.    In order to fix these compile errors I put the keyword static on the FD of the file.  When I run the program in this way I get a runtime error of NullRefernceException -- object reference not set to an instance of an object. 

Now I realize that I could make the method as public and my problems would go away, but I really don't want to have to do this.  How can I resolve this issue?  Thanks

Tags:

Parents
  • Can you show me an example of how you are using this file?
    On what statement are you getting the null reference error?

    The following works fine in my test program and the file opens correctly and writes the record when the static method is called:

     

           class-id teststaticfile.Form1 is partial
                     inherits type System.Windows.Forms.Form.
    
               select test-file assign to "testfile.dat"
                                organization is indexed
                                access is dynamic
                                record key is key1
                                file status is file-status.
           data division.
           file section.
           fd test-file static.
           01 test-record.
              05 key1   pic x(3).
              05 rest   pic x(20).
           working-storage section.
           01 file-status   pic x(2) value spaces static.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent
               goback.
           end method.
    
           method-id statmethod static.
           procedure division.
           
               open output test-file
               display file-status
               move all "1" to test-record
               write test-record
                  invalid key
                     display "bad"
               end-write
               close test-file.
               goback.
               
           end method.
           method-id button1_Click final private.
           procedure division using by value sender as object e as type System.EventArgs.
               
               invoke type teststaticfile.Form1::statmethod
               
           end method.
          
           end class.
    
Reply
  • Can you show me an example of how you are using this file?
    On what statement are you getting the null reference error?

    The following works fine in my test program and the file opens correctly and writes the record when the static method is called:

     

           class-id teststaticfile.Form1 is partial
                     inherits type System.Windows.Forms.Form.
    
               select test-file assign to "testfile.dat"
                                organization is indexed
                                access is dynamic
                                record key is key1
                                file status is file-status.
           data division.
           file section.
           fd test-file static.
           01 test-record.
              05 key1   pic x(3).
              05 rest   pic x(20).
           working-storage section.
           01 file-status   pic x(2) value spaces static.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent
               goback.
           end method.
    
           method-id statmethod static.
           procedure division.
           
               open output test-file
               display file-status
               move all "1" to test-record
               write test-record
                  invalid key
                     display "bad"
               end-write
               close test-file.
               goback.
               
           end method.
           method-id button1_Click final private.
           procedure division using by value sender as object e as type System.EventArgs.
               
               invoke type teststaticfile.Form1::statmethod
               
           end method.
          
           end class.
    
Children
No Data