Highlighted
Contributor.
Contributor.
2023 views

Cobol .Net Exception Handling

Jump to solution

Why does the exception handling var myex always reference "object reference not set to an instance of object"  

Catch myex as type Exception
set str = str::Append("Source " & myex::Source)
set str = str::Append("Number " & myex::StackTrace)
set str = str::Append("Line Number " & myex::TargetSite)
set str = str::Append("Data Exception " & myex::GetType::ToString)
invoke type Debug::WriteLine(str)

0 Likes
1 Solution

Accepted Solutions
Highlighted
Contributor.
Contributor.

RE: Cobol .Net Exception Handling

Jump to solution

I found a solution.  It seems that when you store the connection string in the app.config file, the MySqlException class does not work as expected.  However, if you store the connection string in the settings file, it will catch the MySqlException or SQLException depending on which on used.  See below.

Try

          perform using connstring as type MySqlConnection = new MySqlConnection(type HospitalManagementSoftware.Properties.Settings::Default::connection)

              perform using da as type MySqlDataAdapter = new MySqlDataAdapter("LoginUser", connstring)

                  set da::SelectCommand::CommandType = type CommandType::StoredProcedure

                  invoke connstring::Open()    

                  set t = new DataTable()

                  invoke da::Fill(t)

                  set dataGridView1::DataSource = t

              end-perform

          end-perform

          Catch myex as type MySqlException

              set str to str::Append("Source " & myex::Source::ToString)

              set str to str::Append("Number " & myex::StackTrace::ToString)

              set str to str::Append("Line Number " & myex::TargetSite::ToString)

              set str to str::Append("Data Exception " & myex::GetType::ToString)

              invoke type Debug::WriteLine(str)

              set log to myex::LogString(msg)

              invoke type Debug::WriteLine(log)

          end-try.

View solution in original post

0 Likes
10 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Cobol .Net Exception Handling

Jump to solution

Try the following:

         Catch myex as type Exception

             set str to "Source " & myex::Source

             set str = str & "Number " & myex::StackTrace

             set str = str & "Line Number " & myex::TargetSite

             set str = str & "Data Exception " & myex::GetType::ToString

             invoke type System.Diagnostics.Debug::WriteLine(str)

          end-try.

0 Likes
Highlighted
Contributor.
Contributor.

RE: Cobol .Net Exception Handling

Jump to solution

Sorry Chris, I should have been a little more explicit.  This is in visual studio for starters.  I am using the Stringbuilder class where I am referencing the var str so that part works fine.  When I set a breakpoint and step through the code, as I hover over myex it references the "object reference not set....".

0 Likes
Highlighted
Contributor.
Contributor.

RE: Cobol .Net Exception Handling

Jump to solution

Never mind, I figured it out.  It is truly the exception "object reference not set..."   I am testing my Try Catch statement with several different exception types.  In this particular case, my database connection string was incorrect.  

However, if I try to use

Catch mysqlex as type MySqlException

then it does not go to the catch but stops at the Try statement where it tries to open the connection.  If I specify the type as MySqlException then why doesn't it fall to the catch?  I have used this type of Catch before in C# code, but instead of MySqlException it was the SQLException handling type.

Thoughts?

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Cobol .Net Exception Handling

Jump to solution

How are you doing your database connection?

I would think that you should be able to trap the MySqlException if you are connecting directly using the MySqlConnection  class but it probably would not work if you are using EXEC SQL CONNECT TO syntax

0 Likes
Highlighted
Contributor.
Contributor.

RE: Cobol .Net Exception Handling

Jump to solution

I have the connection string info in the app.config file.  Here is the connection code.  If you have any suggestions, please let me know.  I come from a C#/VB .net background and I am teaching myself the .net cobol so this code is just for practice purposes.  I took Cobol classes in 1984 so I am somewhat familiar with the older standard Cobol syntax.

method-id btnSubmit_Click final private (sender as object e as type System.EventArgs).                 

           Try

           perform using connstring as type MySqlConnection = new MySqlConnection(type ConfigurationManager::ConnectionStrings["conn"]::ConnectionString)

               perform using da as type MySqlDataAdapter = new MySqlDataAdapter("LoginUser", connstring)

                   set da::SelectCommand::CommandType = type CommandType::StoredProcedure

                   invoke connstring::Open()   

                   declare t = new DataTable()

                   invoke da::Fill(t)

                   set dataGridView1::DataSource = t

               end-perform

           end-perform

           Catch myex as type MySqlException

               set str to str::Append("Source " & myex::Source)

               set str to str::Append("Number " & myex::StackTrace)

               set str to str::Append("Line Number " & myex::TargetSite)

               set str to str::Append("Data Exception " & myex::GetType::ToString)

               invoke type Debug::WriteLine(str)

          End-Try

So when I use the exception type MySqlException, the code will break at the first perform using with the "object not set..." error.  This error does seem to be legitimate as I wrote this same code using C# and got the same results.  My question is why does it fall through to the Catch statement when the exception is MySqlException?

0 Likes
Highlighted
Contributor.
Contributor.

RE: Cobol .Net Exception Handling

Jump to solution

Sorry, correction.  My question is why doesn't it fall through to the Catch statement when the exception type is MySqlException.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Cobol .Net Exception Handling

Jump to solution

I don't have MySQL readily available but I tested this same thing with the SQL Server SqlClient provider and the exception handling works for me. If I pass an invalid connection string it jumps to the catch if it is ok it just continues.

This is what I tried:

           declare str as string
           declare connectionString as string = "Data Source=local;Initial Catalog=Northwind;"
                & "Integrated Security=true;"
           try
              perform using connection as type SqlConnection = new SqlConnection
                 set connection::ConnectionString = connectionString
                 invoke connection::Open
                 display "no exception"
           end-perform
           Catch myex as type SqlException
              set str to "Source " & myex::Source
              set str = str & "Number " & myex::StackTrace
              set str = str & "Line Number " & myex::TargetSite
              set str = str & "Data Exception " & myex::GetType::ToString
              invoke type System.Diagnostics.Debug::WriteLine(str)
           end-try
           
           display "after try"
           goback.
0 Likes
Highlighted
Contributor.
Contributor.

RE: Cobol .Net Exception Handling

Jump to solution

That does work indeed.  I am finding that however when you set your connection string in the app.config file the catch exception is "object not set to an instance..." even for type SQLException.  Or even if you have the following you will get the same exception message "object not set..."

declare str as string

      declare connectionString as string = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=HospitalMgt;Integrated Security=True;"

          try

             perform using connection as type SqlConnection = new SqlConnection(connectionString)::ConnectionString

                invoke connection::Open

0 Likes
Highlighted
Contributor.
Contributor.

RE: Cobol .Net Exception Handling

Jump to solution

I found a solution.  It seems that when you store the connection string in the app.config file, the MySqlException class does not work as expected.  However, if you store the connection string in the settings file, it will catch the MySqlException or SQLException depending on which on used.  See below.

Try

          perform using connstring as type MySqlConnection = new MySqlConnection(type HospitalManagementSoftware.Properties.Settings::Default::connection)

              perform using da as type MySqlDataAdapter = new MySqlDataAdapter("LoginUser", connstring)

                  set da::SelectCommand::CommandType = type CommandType::StoredProcedure

                  invoke connstring::Open()    

                  set t = new DataTable()

                  invoke da::Fill(t)

                  set dataGridView1::DataSource = t

              end-perform

          end-perform

          Catch myex as type MySqlException

              set str to str::Append("Source " & myex::Source::ToString)

              set str to str::Append("Number " & myex::StackTrace::ToString)

              set str to str::Append("Line Number " & myex::TargetSite::ToString)

              set str to str::Append("Data Exception " & myex::GetType::ToString)

              invoke type Debug::WriteLine(str)

              set log to myex::LogString(msg)

              invoke type Debug::WriteLine(log)

          end-try.

View solution in original post

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Cobol .Net Exception Handling

Jump to solution

The app.config version works fine for me with SQL Server. If the connection string is OK, it connects and all is good. If the connection string is formatted correctly but contains an invalid Data Source then I get a SQLException on the connect. If I have an invalid entry in the connection string such as a typo then an ArgumentException is thrown and the SQLException will not be caught.

On what statement do you see an exception, on the connect or on the read from app.config?

Is it possible that you are not getting a SQLException because it is not actually a SQL error being reported?

I also found a reference to the null object error occurring if you don't have a reference to the System.Configuration.dll in your project although mine will not even compile without this,

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.