locked
System.NullReferenceException was unhandled RRS feed

  • Question

  • I don't know what's going on anymore!!  I used this code in another application and it was working fine!!  Now I get this error...

    public  System.Data.DataTable GetSASTable(string sasTable, stringquery)        {

                // Create a connection

                OleDbConnection cn = new OleDbConnection();

     

                string connectionString = "Provider=sas.IOMProvider.1; SAS Workspace ID="

                + Common.SASConnection.Connection.supportSpace.UniqueIdentifier + ";";

    •  

        Error again!!  System.NullReferenceException was unhandled

     

                cn.ConnectionString = connectionString;

                OleDbCommand cmd = cn.CreateCommand();

                cn.Open();

                //Assign libref "cansimuc" to point to the SAS tables

                cmd.CommandText = "libname cansimuc '" + Common.SASConnection.Connection.outSupportPath + "/sasdata';";

                cmd.ExecuteNonQuery();

                DataTable table;

                System.Data.DataSet dataset = new System.Data.DataSet();

                OleDbDataAdapter adapter = new OleDbDataAdapter(query, connectionString);

                adapter.Fill(dataset, "cansimuc." + sasTable);

                table = dataset.Tables[0];

                cn.Close();

                cmd.Dispose();

                return table;

            }


    M. Chalut
    Wednesday, March 30, 2011 9:02 PM

Answers

  • It's a NullReferenceException. The problem is one of the fields or properties returns null in:

    Common.SASConnection.Connection.supportSpace.UniqueIdentifier

    I suppose Common.SASConnection is a class. Are Connection and supportSpace initialized?


    • Marked as answer by Mylen33 Thursday, March 31, 2011 5:16 PM
    Thursday, March 31, 2011 1:49 PM

All replies

  • Where are you getting the exception in this code?  Without having your database, etc, I'm just guessing, but some things I'd recommend trying:

    1) don't create your command until you open your connection.

    3) dispose your command before your connection.

    2) use using blocks on the reader, command, and connection - this makes sure they close on exception and that they close in the proper order:

     

    var connectionString = ....
    
    using (var cn = new OleDbConnection())
    {
     cn.ConnectionString = connectionString;
     cn.Open();
    
     using (var cmd = cn.CreateCommand())
     {
      cmd.CommandText = ...
      cmd.ExecuteNonQuery();
     }
    }
    
    
    

     

     


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...



    Wednesday, March 30, 2011 9:20 PM
  • Where exactly in the code do you get the error?

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Thursday, March 31, 2011 10:35 AM
  • Sorry...    here is where I get the error...

    string connectionString = "Provider=sas.IOMProvider.1; SAS Workspace ID=" + Common.SASConnection.Connection.supportSpace.UniqueIdentifier + ";";

    Error again!!  System.NullReferenceException was unhandled


    M. Chalut
    Thursday, March 31, 2011 12:10 PM
  • Did you initialize this object?

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Thursday, March 31, 2011 12:16 PM
  • You mean

    private string connectionString = string.Empty;

     

     


    M. Chalut
    Thursday, March 31, 2011 1:42 PM
  • Your connection should have the at before any " "...

    string connectionString = @"Provider=sas.IOMProvider.1; SAS Workspace ID=" + Common.SASConnection.Connection.supportSpace.UniqueIdentifier + ";";


    Just Be Humble Malange!
    Thursday, March 31, 2011 1:45 PM
  • It's a NullReferenceException. The problem is one of the fields or properties returns null in:

    Common.SASConnection.Connection.supportSpace.UniqueIdentifier

    I suppose Common.SASConnection is a class. Are Connection and supportSpace initialized?


    • Marked as answer by Mylen33 Thursday, March 31, 2011 5:16 PM
    Thursday, March 31, 2011 1:49 PM
  • Agreed.  One of those must be null.  Either step through in a debugger or log the state of each one to see which is null.

    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    Thursday, March 31, 2011 2:04 PM
  • You could use asserts, console writes, or a log to quickly pin which is null, then determine why based on your config for that SAS connection.

    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    Thursday, March 31, 2011 3:32 PM
  • Hello Mylen,

    The exception is thrown because of this :

    Common.SASConnection.Connection.supportSpace.UniqueIdentifier
    

    You should debug your code and see which object here is null.

    Kind regards,

    Thursday, March 31, 2011 4:48 PM
  • You were right guys...  my supportSpace values was not tranfered properly.  Got it now.

    Thanks a lot!!


    M. Chalut
    Thursday, March 31, 2011 5:16 PM