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:

  • 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.
    
  • Below is the code.  I just thought of something.  I was a little misleading because this is a class was added as a new item to a project that is using winforms.  I wouldn't think that would make much of a difference though.

    The exception happens when I try and open the return-file. 

    The compile errors occur when I remove the keyword static from the FD.  Thanks for your help.

          $set sourceformat(free)

          class-id BeneScreens.beneMethods.

          Environment division.

              select return-file

                  assign       to "c:\temp\returnfilebene.wrk"

                  organization is line sequential.

          file section.

          fd  return-file static.

          01  return-rec.

              05  rr-pid-number              pic  x(06).

              05  rr-button                  pic  x(10).

          working-storage section.

          method-id InstanceMethod.

          local-storage section.

          procedure division.

              goback.

          end method.

          method-id goToPid public static.

          local-storage section.

              open input return-file. 

              read return-file

              if rr-button = "cancel"            

                  set fromPidNum                     to goToPidPidNum

              else

                  if rr-button = "ok"

                      set fromPidNum                 to rr-pid-number.

              close return-file.

          end method.

          end class. 

  • I just added your code to a new class in the same project.

    I had to remove the two references to the fromPidNum and the goToPidPidNum because these data items are not defined.

    I added the following to my forms class:

    invoke type BeneScreens.beneMethods::goToPid

    and it invokes the method successfully and opens the file and returns.

    I just used a dummy text file named c:\temp\returnfilebene.wrk and it all worked ok.

    What product version are you using?

    Go to Help-->About Microsoft Visual Studio and then select Visual COBOL in the list.

    What is the version number?

    Thanks.

  • Yeah, sorry about those two fields.  I trimmed out what wasn't necessary.  Below is the information that I believe you are looking for.

     

     

    Microsoft Visual Studio 2010 Version 10.0.40219.1 SP1Rel Microsoft .NET Framework Version 4.5.50709 SP1Rel

    Installed Version: IDE Standard

    Microsoft Visual Web Developer 2010   01011-532-2002361-70156 Microsoft Visual Web Developer 2010

    Hotfix for Visual Studio 2010 Shell (Integrated) - ENU (KB2549864)   KB2549864 This hotfix is for Visual Studio 2010 Shell (Integrated) - ENU. If you later install a more recent service pack, this hotfix will be uninstalled automatically. For more information, visit support.microsoft.com/.../2549864.

    Hotfix for Visual Studio 2010 Shell (Integrated) - ENU (KB2635973)   KB2635973 This hotfix is for Visual Studio 2010 Shell (Integrated) - ENU. If you later install a more recent service pack, this hotfix will be uninstalled automatically. For more information, visit support.microsoft.com/.../2635973.

    Micro Focus Visual COBOL 2.2   Serial number: 110000205879 Micro Focus Visual COBOL 2.2 Version 2.2.00244. Serial number: 110000205879 Full license For more information, visit http://www.microfocus.com. Copyright (C) Micro Focus 1984-2013. All rights reserved.

    Security Update for Visual Studio 2010 Shell (Integrated) - ENU (KB2645410)   KB2645410 This security update is for Visual Studio 2010 Shell (Integrated) - ENU. If you later install a more recent service pack, this security update will be uninstalled automatically. For more information, visit support.microsoft.com/.../2645410.

    Visual Studio 2010 Shell (Isolated) - ENU Service Pack 1 (KB983509)   KB983509 This service pack is for Visual Studio 2010 Shell (Isolated) - ENU. If you later install a more recent service pack, this service pack will be uninstalled automatically. For more information, visit support.microsoft.com/.../983509.

  • That is the latest version which is 2.2 and is the same version that I am testing with.

    Can you please open up a support incident for this and put my name in the description so that it comes to me?

    I will need you to zip up your full project (or better yet a cutdown) and attach it to the incident so that I can try to reproduce the problem here.

    Thanks.

  • I decided to give your code a try (below) that you submitted and when I did that I got an error message when I tried to create the solution -- see attached.  I'm thinking that I will un-install the 2.2 and reinstall it before I submit a ticket, unless you think otherwise.

     

           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.
  • Did you have an earlier product release installed that you updated to the 2.2 release or did you install the 2.2 release as the first Visual COBOL product on this system?

    Under what OS are you running?

    Yes, go ahead and uninstall and reinstall the product and see if that makes a difference.

  • I had an earlier product release and then just this week I installed the 2.2.  I'm on Windows 7 Professional Service Pack 1.  I did un-install both the 2.2 release and the data tools 2.2 and I am still having problems in my program. It should also be noted that a co-worker is running pretty much the same software and he is having issues.

    I accidently selected NO on that error message that I sent you earlier (attached), do you know how to turn that back on? 

  • Did you mean "a co-worker is running pretty much the same software and he is NOT having issues."

    Try uninstalling Visual COBOL 2.2 and then also uninstalling the Visual Studio 2010 Shell and then reinstalling Visual COBOL 2.2 so that it reinstalls the Visual Studio Shell as well.

    Thanks.

  • No, a co-worker has pretty much the same software installed on his pc as mine and he IS having issues.  He is running win 7 sp1 with vs 2010 shell and visual cobol 2.2.  I will un-install the software as you suggested and get back to you. 

    One more thing in my previous post I asked if you knew how to get that error message to return as I accidently clicked for it to not return (that is what you get when you get into a hurry).  Thanks