Object reference not set to an instance of an object.


G

Guest

OS Name Microsoft Windows XP Professional
Version 5.1.2600 Service Pack 2 Build 2600
Total Physical Memory 1,024.00 MB

MDE 2003 Version 7.1.3008
..NET Framework 1.1 Version 1.1.4322 SP1
Microsoft Visual C# .NET 69462-335-0000007-18707
Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3


Code Description:
The main form does the following:
sets up adata adapter adpTurbine10MinData
Creates and filles two dataset with various unchanging configuration
information
Based on the configuration instantiates:
V47 objects
event handlers to service delegates from each V47 object
Instantiate a 10 minute timer WTGData_10Timer

The event handler WTGDataRowReady10Handler is called by a delegate in the
V47 object every 10
10 minutes. To be fast ( because there could be hundreds of V47 objects) the
handler adds the
the data struct to an collection and returns. Once all the V47 objects add
their data to the collection the timer
WTGData_10Timer is allowed to complete and onCalcTimedEvent is called.

onCalcTimedEvent tests that there are no duplicates in the collection and the
data is added to a data set. Once the data set is complete, it is written to
SQL server.

The "Object reference not set to an instance of an object." occures in
onCalcTimedEvent.

Note that the first 10 minute call works correctly and and the dataset is
written
correctly. All the following 10 minute calls result in a "Object reference
not set to an instance of an object." Error



Code snippets as follows:

//***********************************************************
// Main Form application
//***********************************************************
namespace Trillium.WTGCube
{
public class frmWTGCube : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;

// create a dataset for WTG 10 minute data
private DataSet DS10MinData = new DataSet("DSTurbine10MinData");
private ArrayList WTGData_10 = new ArrayList();


private System.Timers.Timer WTGData_10Timer = new System
..Timers.Timer(); // services 10 minute data write to the SQL Server

public frmWTGCube()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// Constructor code
//

// DataAdapter
SqlDataAdapter adpTurbine10MinData = new SqlDataAdapter(
"SELECT * FROM Turbine10MinuteData", "workstation id=" + WorkstationId +
";packet size=4096;integrated security=SSPI;data source=" +
SqlServerDataSource +
";persist security info=False;initial catalog=TrilliumWind");
try
{
// Set the schema
adpTurbine10MinData.FillSchema(DS10MinData, SchemaType.Source,
"Turbine10MinuteData");

}
catch( SqlException ex)
{
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Can't connect to TrilliumWind Db, SQL Exception -"
+
ex.Message);
}
catch (Exception ex)
{
// catch all other exceptions
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Can't connect to TrilliumWind Db - " +
ex.Message);
}
finally
{
adpTurbine10MinData.Dispose();
}
// create a dataset for WTG Configuration
DataSet DSWTG = new DataSet("WTGConfig");
// create a dataset for Tag Names
DataSet DSTagNames = new DataSet("TagNames");

TrilliumGeneral.ReadWTGConfig(ref DSWTG,
Trillium.Utility.TrilliumConstants.WTGLog,
WorkstationId, SqlServerDataSource );
TrilliumGeneral.ReadTagNames(ref DSTagNames,
Trillium.Utility.TrilliumConstants.WTGLog,
WorkstationId, SqlServerDataSource );

if ( DSWTG == null || DSTagNames == null)
{
// failure to load the data set!!!
WriteToEventLogs.WTGLogWrite( "WindSpace- Abnormal End - Could not build
DataSets");
}

// initialize the WTGs
// for each turbine
foreach ( DataRow TagNameRow in DSTagNames.Tables["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in DSWTG.Tables["TurbineConfiguration"].Rows )
{
switch ( ConfigRow["Turbine Type"].ToString())
{

case "V47":
// instantiate a new turbine
VestasWTG V47 = new VestasWTG( TagNameRow, ConfigRow);

// There are several other delegates instantiated that are not shown

// 10 min datarow delegate
V47.WTGDataRowReady_10 += new
Trillium.WindSpace.VestasWTG.WTGDataRowReady10Delegate(WTGDataRowReady10Handler);

break;
default:
break;
}
}
}

WTGData_10Timer.Elapsed += new ElapsedEventHandler(onCalcTimedEvent); //
handles writing the 10 minute data Collection to the SQL Server
}

private void WTGDataRowReady10Handler(
Trillium.WindSpace.WindSpace.WTGDataGroup_10 myWTGData )
{
WTGData_10Timer.Enabled = false;
try
{
WTGData_10.Add(myWTGData);
}
catch( Exception ex)
{
Utility.WriteToEventLogs.logWrite("WTGDataRowReady10Handler: " +
ex.Message, Utility.TrilliumConstants.WTGLog);
}
WTGData_10Timer.Interval = 9999;
WTGData_10Timer.Enabled = true;

}
public void onCalcTimedEvent(object source, ElapsedEventArgs e)
{
// local storage for each turbines 10 min data
Trillium.WindSpace.WindSpace.WTGDataGroup_10 myWTGData = new
Trillium.WindSpace.WindSpace.WTGDataGroup_10();

WTGData_10Timer.Enabled = false;

// Add each row from the collection WTGData_10 to the data set
// WTGData_10 is the collecttion that has been filled by the event handler
// WTGDataRowReady10Handler. A row for each turbine.

// get a copy of the first row
myWTGData = (Trillium.WindSpace.WindSpace.WTGDataGroup_10) WTGData_10[0];

// add it to the empty DS10MinData
DS10MinData.Clear();
addData(myWTGData);

// add the rest, checking that there are no dups
for(int i = 1; i < WTGData_10.Count; i++)
{
bool bDupFlag = false;
try
{
myWTGData = (Trillium.WindSpace.WindSpace.WTGDataGroup_10)WTGData_10;

foreach ( DataRow DR in DS10MinData.Tables["Turbine10MinuteData"].Rows)
{
//["Date"] = myWTGData.dt;
//["Turbine ID"] = myWTGData.TurbineID;
//["Circuit ID"] = myWTGData.TurbineCircuitID;

// test if myWTGData row equals anything already in the Dataset
DS10MinData
if ( (myWTGData.TurbineID == Convert.ToInt32(DR["Turbine ID"])) &&
(myWTGData.TurbineCircuitID == Convert.ToInt32(DR["Circuit ID"])) &&
(myWTGData.dt == Convert.ToDateTime(DR["Date"])) )
{
//yes this myWTGData has a duplicate in the data set - don't add it
bDupFlag = true;
break;
}
}
}
catch( Exception ex)
{
****ERROR handled here *****
Utility.WriteToEventLogs.logWrite("onCalcTimedEvent: testing for dups
error: " +
ex.Message, Utility.TrilliumConstants.WTGLog);
}
// if this is not a duplicate add it to the Data set
if ( !bDupFlag ) addData(myWTGData);
bDupFlag = false;

// go on to the next element in the collection WTGData_10
}


Utility.WriteToEventLogs.logWrite("onCalcTimedEvent: DataRows in
DS10MinData.Tables: " +
DS10MinData.Tables["Turbine10MinuteData"].Rows.Count.ToString(),
Utility.TrilliumConstants.WTGLog);

// send the dataset to the data base
try
{
#region "SQL stuff"
// code not shown
#endregion

adpTurbine10MinData.Update(DS10MinData);
adpTurbine10MinData.Dispose();
}
catch( SqlException ex)
{
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEventonCalcTimedEvent-Can't write to to TrilliumWind Db,
SQL Exception -" +
ex.Message);
}
catch (Exception ex)
{
// catch all other exceptions
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Error writing to TrilliumWind Db - " +
ex.Message.ToString());
}

// remove all elements for the DS and the collection
DS10MinData.Clear();
WTGData_10.Clear();
}
private void addData( Trillium.WindSpace.WindSpace.WTGDataGroup_10
myWTGData)
{
// data row names
// ["Wind Speed"]
// ["Wind Direction"]
// ["Power"]
// ["Expected Power"]
// ["Energy"]
// ["Efficiency"]
// ["Status Fault"]
// ["Status Offline"]
// ["Status Running"]
// ["Status Comm Err"]
// ["Status Curtailed"]
// ["Status Eddy Wind"]
// ["DownTime Catagory"]
// ["Communications"]
// ["Events"]
DataRow ROW = DS10MinData.Tables["Turbine10MinuteData"].NewRow();

ROW["Date"] = myWTGData.dt;
ROW["Turbine ID"] = myWTGData.TurbineID;
ROW["Circuit ID"] = myWTGData.TurbineCircuitID;
ROW["Wind Speed"] = myWTGData.WindSpeed_10;
ROW["Wind Direction"] = myWTGData.WindDirr_10;
ROW["Power"] = myWTGData.Power_10;
ROW["Expected Power"] = myWTGData.Expected_Power;
ROW["Energy"] = myWTGData.Energy_10;
ROW["Efficiency"] = myWTGData.Efficiency;
ROW["Status Fault"] = myWTGData.Status_Fault;
ROW["Status Offline"] = myWTGData.Status_Offline;
ROW["Status Running"] = myWTGData.Status_Running;
ROW["Status Comm Err"] = myWTGData.Status_Comm_Err;
ROW["Status Curtailed"] = myWTGData.Status_Curtailed;
ROW["Status Eddy Wind"] = myWTGData.Status_Eddy_Wind;
ROW["DownTime Catagory"]= myWTGData.DownTime_Catagory;
ROW["Communications"] = 0;
ROW["Events"] = 0;

DS10MinData.Tables["Turbine10MinuteData"].Rows.Add(ROW);
}

} // end public class frmWTGCube : System.Windows.Forms.Form

} // end namespace Trillium.WTGCube


namespace Trillium.WindSpace
{

public class WindSpace : IDisposable
{
public WindSpace()
{
}
protected DataSet DSObjInstance = new DataSet();

// Timer for one second calculations
protected Timer _OneSecTimer = new Timer();
protected Timer _OneSecDTTimer = new Timer();

// timer for clock 1 minute and ten minute calculations
protected Timer _CalcTimer = new Timer();

// 10 minute WTG averages and data
public struct WTGDataGroup_10
{
public DateTime dt;
public int TurbineCircuitID;
public int TurbineID;
public string TurbineName;
public float Energy_10;
public float WindSpeed_10;
public float WindDirr_10;
public float Power_10;
public float Efficiency;
public float Expected_Power;
public int Status_Comm_Err;
public int Status_Running;
public int Status_Offline;
public int Status_Fault;
public int Status_Curtailed;
public int Status_Eddy_Wind;
public int DownTime_Catagory;
}
} // end public class WindSpace

public class VestasWTG : WindSpace
{
private bool bCalcFunctionsDone = false;

// one second data refresh
public delegate void WTGDataReadyDelegate(WTGDataGroup w);
public event WTGDataReadyDelegate WTGDataReady;

// one minute data refresh
public delegate void WTGDataReady1Delegate(WTGDataGroup_1 x);
public event WTGDataReady1Delegate WTGDataReady_1;

// ten minute data for SQL Server
public delegate void WTGDataRowReady10Delegate(WTGDataGroup_10 z);
public event WTGDataRowReady10Delegate WTGDataRowReady_10;

public delegate void WTGDTReadyDelegate(DowntimeData v);
public event WTGDTReadyDelegate WTGDTReady;

public VestasWTG(DataRow TagNames, DataRow Configs)
{
//
// Vestas WTG Constructor
//

// timer for 1 sec data and calculations
this._OneSecTimer.Elapsed+=new ElapsedEventHandler(this.oneSecTimerEvent);
this._OneSecTimer.Interval = 999;

// timer for 1 min and 10 min data and calcs
this._CalcTimer.Elapsed += new ElapsedEventHandler(onCalcTimedEvent);
this._CalcTimer.Interval = 20;

// timer for 1 sec Downtime calculations
this._OneSecDTTimer.Elapsed+=new ElapsedEventHandler(this.oneSecDTEvent);
this._OneSecDTTimer.Interval = 999;

//-------------------------------------
// various code
//-------------------------------------

// Enable Calculations
this._OneSecTimer.Enabled = true;
this._CalcTimer.Enabled = true;
this._OneSecDTTimer.Enabled = true;


}

//
// ********************************************************
// onCalcTimedEvent
//
// Functions:
// - call v1MinFunctions - clock 1 minute function
// - calls v10MinFunctions - clock 10 minute functions
//
// - the call occurs on the computers clock minute and 10 minutes
//
//*********************************************************
private void onCalcTimedEvent(object source, ElapsedEventArgs e)
{
DateTime dt = DateTime.Now;

// test if we're at 59.500 sec
if (dt.Second == 59 && dt.Millisecond > 500 )
{
// write out 1 min data
if (!this.bCalcFunctionsDone)
{
this.bCalcFunctionsDone = true;

v1MinFunctions();

// test for ten minute data
if( dt.Minute == 9 ||dt.Minute == 19 || dt.Minute == 29 ||
dt.Minute == 39 || dt.Minute == 49 ||dt.Minute == 59 )
{
v10MinFunctions();
} // end if
}
}
else this.bCalcFunctionsDone = false;
}

// 10 minute functions
public void v10MinFunctions()
{
WTGDataGroup_10 myWTGDataGroup_10 = new WTGDataGroup_10();
float ftemp = 0.0F;
DataRow ROW, READROW;

myWTGDataGroup_10.WindSpeed_10 = 0.0F;
myWTGDataGroup_10.WindDirr_10 = 0.0F;
myWTGDataGroup_10.Power_10 = 0.0F;
myWTGDataGroup_10.Efficiency = 0.0F;
myWTGDataGroup_10.Expected_Power = 0.0F;
myWTGDataGroup_10.Status_Comm_Err = 0;
myWTGDataGroup_10.Status_Running = 0;
myWTGDataGroup_10.Status_Offline = 0;
myWTGDataGroup_10.Status_Fault = 0;
myWTGDataGroup_10.Status_Curtailed = 0;
myWTGDataGroup_10.Status_Eddy_Wind = 0;
myWTGDataGroup_10.DownTime_Catagory = 0;


// Set up Turbine identification in myWTGDataGroup
myWTGDataGroup_10.dt = DateTime.Now;
myWTGDataGroup_10.TurbineCircuitID = _iCircuitID;
myWTGDataGroup_10.TurbineID = _iTurbineID;
myWTGDataGroup_10.TurbineName = _strTurbineName;

//--------------------------------
// do calculations to fill up WTGDataGroup_10
//--------------------------------

// send out 10 minute delegates
// data row delegate
int iRetry = 0;
bool bRetryFlag = false;
string strEx="";
while ( iRetry < 3)
{
try
{
WTGDataRowReady_10(myWTGDataGroup_10);
}
catch( Exception ex)
{
bRetryFlag = true;
strEx = ex.Message;
}
if ( bRetryFlag )
{
iRetry++;
bRetryFlag = false;
}
else break;
}
if ( bRetryFlag )
{
// not successful in sending delegate
Utility.WriteToEventLogs.logWrite("v10MinFunctions: WTGDataRowReady_10
delegate error:" +
strEx.ToString(), Utility.TrilliumConstants.WTGLog);
}

// refresh screen delegate
// WTGDataReady_10(myWTGDataGroup_10);


}

} // End public class VestasWTG


} // end namespace Trillium.WindSpace
 
Ad

Advertisements

D

Dale Preston

You may want to look your post over, find out what parts of your code really
are involved in causing the problem and add the details of the error
message. Keep it short and it will be much easier to help.

Dale Preston
MCAD, MCDBA, MCSE


FredC said:
OS Name Microsoft Windows XP Professional
Version 5.1.2600 Service Pack 2 Build 2600
Total Physical Memory 1,024.00 MB

MDE 2003 Version 7.1.3008
.NET Framework 1.1 Version 1.1.4322 SP1
Microsoft Visual C# .NET 69462-335-0000007-18707
Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3


Code Description:
The main form does the following:
sets up adata adapter adpTurbine10MinData
Creates and filles two dataset with various unchanging configuration
information
Based on the configuration instantiates:
V47 objects
event handlers to service delegates from each V47 object
Instantiate a 10 minute timer WTGData_10Timer

The event handler WTGDataRowReady10Handler is called by a delegate in the
V47 object every 10
10 minutes. To be fast ( because there could be hundreds of V47 objects) the
handler adds the
the data struct to an collection and returns. Once all the V47 objects add
their data to the collection the timer
WTGData_10Timer is allowed to complete and onCalcTimedEvent is called.

onCalcTimedEvent tests that there are no duplicates in the collection and the
data is added to a data set. Once the data set is complete, it is written to
SQL server.

The "Object reference not set to an instance of an object." occures in
onCalcTimedEvent.

Note that the first 10 minute call works correctly and and the dataset is
written
correctly. All the following 10 minute calls result in a "Object reference
not set to an instance of an object." Error



Code snippets as follows:

//***********************************************************
// Main Form application
//***********************************************************
namespace Trillium.WTGCube
{
public class frmWTGCube : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;

// create a dataset for WTG 10 minute data
private DataSet DS10MinData = new DataSet("DSTurbine10MinData");
private ArrayList WTGData_10 = new ArrayList();


private System.Timers.Timer WTGData_10Timer = new System
.Timers.Timer(); // services 10 minute data write to the SQL Server

public frmWTGCube()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// Constructor code
//

// DataAdapter
SqlDataAdapter adpTurbine10MinData = new SqlDataAdapter(
"SELECT * FROM Turbine10MinuteData", "workstation id=" + WorkstationId +
";packet size=4096;integrated security=SSPI;data source=" +
SqlServerDataSource +
";persist security info=False;initial catalog=TrilliumWind");
try
{
// Set the schema
adpTurbine10MinData.FillSchema(DS10MinData, SchemaType.Source,
"Turbine10MinuteData");

}
catch( SqlException ex)
{
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Can't connect to TrilliumWind Db, SQL Exception -"
+
ex.Message);
}
catch (Exception ex)
{
// catch all other exceptions
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Can't connect to TrilliumWind Db - " +
ex.Message);
}
finally
{
adpTurbine10MinData.Dispose();
}
// create a dataset for WTG Configuration
DataSet DSWTG = new DataSet("WTGConfig");
// create a dataset for Tag Names
DataSet DSTagNames = new DataSet("TagNames");

TrilliumGeneral.ReadWTGConfig(ref DSWTG,
Trillium.Utility.TrilliumConstants.WTGLog,
WorkstationId, SqlServerDataSource );
TrilliumGeneral.ReadTagNames(ref DSTagNames,
Trillium.Utility.TrilliumConstants.WTGLog,
WorkstationId, SqlServerDataSource );

if ( DSWTG == null || DSTagNames == null)
{
// failure to load the data set!!!
WriteToEventLogs.WTGLogWrite( "WindSpace- Abnormal End - Could not build
DataSets");
}

// initialize the WTGs
// for each turbine
foreach ( DataRow TagNameRow in DSTagNames.Tables["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in DSWTG.Tables["TurbineConfiguration"].Rows )
{
switch ( ConfigRow["Turbine Type"].ToString())
{

case "V47":
// instantiate a new turbine
VestasWTG V47 = new VestasWTG( TagNameRow, ConfigRow);

// There are several other delegates instantiated that are not shown

// 10 min datarow delegate
V47.WTGDataRowReady_10 += new
Trillium.WindSpace.VestasWTG.WTGDataRowReady10Delegate(WTGDataRowReady10Hand
ler);

break;
default:
break;
}
}
}

WTGData_10Timer.Elapsed += new ElapsedEventHandler(onCalcTimedEvent); //
handles writing the 10 minute data Collection to the SQL Server
}

private void WTGDataRowReady10Handler(
Trillium.WindSpace.WindSpace.WTGDataGroup_10 myWTGData )
{
WTGData_10Timer.Enabled = false;
try
{
WTGData_10.Add(myWTGData);
}
catch( Exception ex)
{
Utility.WriteToEventLogs.logWrite("WTGDataRowReady10Handler: " +
ex.Message, Utility.TrilliumConstants.WTGLog);
}
WTGData_10Timer.Interval = 9999;
WTGData_10Timer.Enabled = true;

}
public void onCalcTimedEvent(object source, ElapsedEventArgs e)
{
// local storage for each turbines 10 min data
Trillium.WindSpace.WindSpace.WTGDataGroup_10 myWTGData = new
Trillium.WindSpace.WindSpace.WTGDataGroup_10();

WTGData_10Timer.Enabled = false;

// Add each row from the collection WTGData_10 to the data set
// WTGData_10 is the collecttion that has been filled by the event handler
// WTGDataRowReady10Handler. A row for each turbine.

// get a copy of the first row
myWTGData = (Trillium.WindSpace.WindSpace.WTGDataGroup_10) WTGData_10[0];

// add it to the empty DS10MinData
DS10MinData.Clear();
addData(myWTGData);

// add the rest, checking that there are no dups
for(int i = 1; i < WTGData_10.Count; i++)
{
bool bDupFlag = false;
try
{
myWTGData = (Trillium.WindSpace.WindSpace.WTGDataGroup_10)WTGData_10;

foreach ( DataRow DR in DS10MinData.Tables["Turbine10MinuteData"].Rows)
{
//["Date"] = myWTGData.dt;
//["Turbine ID"] = myWTGData.TurbineID;
//["Circuit ID"] = myWTGData.TurbineCircuitID;

// test if myWTGData row equals anything already in the Dataset
DS10MinData
if ( (myWTGData.TurbineID == Convert.ToInt32(DR["Turbine ID"])) &&
(myWTGData.TurbineCircuitID == Convert.ToInt32(DR["Circuit ID"])) &&
(myWTGData.dt == Convert.ToDateTime(DR["Date"])) )
{
//yes this myWTGData has a duplicate in the data set - don't add it
bDupFlag = true;
break;
}
}
}
catch( Exception ex)
{
****ERROR handled here *****
Utility.WriteToEventLogs.logWrite("onCalcTimedEvent: testing for dups
error: " +
ex.Message, Utility.TrilliumConstants.WTGLog);
}
// if this is not a duplicate add it to the Data set
if ( !bDupFlag ) addData(myWTGData);
bDupFlag = false;

// go on to the next element in the collection WTGData_10
}


Utility.WriteToEventLogs.logWrite("onCalcTimedEvent: DataRows in
DS10MinData.Tables: " +
DS10MinData.Tables["Turbine10MinuteData"].Rows.Count.ToString(),
Utility.TrilliumConstants.WTGLog);

// send the dataset to the data base
try
{
#region "SQL stuff"
// code not shown
#endregion

adpTurbine10MinData.Update(DS10MinData);
adpTurbine10MinData.Dispose();
}
catch( SqlException ex)
{
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEventonCalcTimedEvent-Can't write to to TrilliumWind Db,
SQL Exception -" +
ex.Message);
}
catch (Exception ex)
{
// catch all other exceptions
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Error writing to TrilliumWind Db - " +
ex.Message.ToString());
}

// remove all elements for the DS and the collection
DS10MinData.Clear();
WTGData_10.Clear();
}
private void addData( Trillium.WindSpace.WindSpace.WTGDataGroup_10
myWTGData)
{
// data row names
// ["Wind Speed"]
// ["Wind Direction"]
// ["Power"]
// ["Expected Power"]
// ["Energy"]
// ["Efficiency"]
// ["Status Fault"]
// ["Status Offline"]
// ["Status Running"]
// ["Status Comm Err"]
// ["Status Curtailed"]
// ["Status Eddy Wind"]
// ["DownTime Catagory"]
// ["Communications"]
// ["Events"]
DataRow ROW = DS10MinData.Tables["Turbine10MinuteData"].NewRow();

ROW["Date"] = myWTGData.dt;
ROW["Turbine ID"] = myWTGData.TurbineID;
ROW["Circuit ID"] = myWTGData.TurbineCircuitID;
ROW["Wind Speed"] = myWTGData.WindSpeed_10;
ROW["Wind Direction"] = myWTGData.WindDirr_10;
ROW["Power"] = myWTGData.Power_10;
ROW["Expected Power"] = myWTGData.Expected_Power;
ROW["Energy"] = myWTGData.Energy_10;
ROW["Efficiency"] = myWTGData.Efficiency;
ROW["Status Fault"] = myWTGData.Status_Fault;
ROW["Status Offline"] = myWTGData.Status_Offline;
ROW["Status Running"] = myWTGData.Status_Running;
ROW["Status Comm Err"] = myWTGData.Status_Comm_Err;
ROW["Status Curtailed"] = myWTGData.Status_Curtailed;
ROW["Status Eddy Wind"] = myWTGData.Status_Eddy_Wind;
ROW["DownTime Catagory"]= myWTGData.DownTime_Catagory;
ROW["Communications"] = 0;
ROW["Events"] = 0;

DS10MinData.Tables["Turbine10MinuteData"].Rows.Add(ROW);
}

} // end public class frmWTGCube : System.Windows.Forms.Form

} // end namespace Trillium.WTGCube


namespace Trillium.WindSpace
{

public class WindSpace : IDisposable
{
public WindSpace()
{
}
protected DataSet DSObjInstance = new DataSet();

// Timer for one second calculations
protected Timer _OneSecTimer = new Timer();
protected Timer _OneSecDTTimer = new Timer();

// timer for clock 1 minute and ten minute calculations
protected Timer _CalcTimer = new Timer();

// 10 minute WTG averages and data
public struct WTGDataGroup_10
{
public DateTime dt;
public int TurbineCircuitID;
public int TurbineID;
public string TurbineName;
public float Energy_10;
public float WindSpeed_10;
public float WindDirr_10;
public float Power_10;
public float Efficiency;
public float Expected_Power;
public int Status_Comm_Err;
public int Status_Running;
public int Status_Offline;
public int Status_Fault;
public int Status_Curtailed;
public int Status_Eddy_Wind;
public int DownTime_Catagory;
}
} // end public class WindSpace

public class VestasWTG : WindSpace
{
private bool bCalcFunctionsDone = false;

// one second data refresh
public delegate void WTGDataReadyDelegate(WTGDataGroup w);
public event WTGDataReadyDelegate WTGDataReady;

// one minute data refresh
public delegate void WTGDataReady1Delegate(WTGDataGroup_1 x);
public event WTGDataReady1Delegate WTGDataReady_1;

// ten minute data for SQL Server
public delegate void WTGDataRowReady10Delegate(WTGDataGroup_10 z);
public event WTGDataRowReady10Delegate WTGDataRowReady_10;

public delegate void WTGDTReadyDelegate(DowntimeData v);
public event WTGDTReadyDelegate WTGDTReady;

public VestasWTG(DataRow TagNames, DataRow Configs)
{
//
// Vestas WTG Constructor
//

// timer for 1 sec data and calculations
this._OneSecTimer.Elapsed+=new ElapsedEventHandler(this.oneSecTimerEvent);
this._OneSecTimer.Interval = 999;

// timer for 1 min and 10 min data and calcs
this._CalcTimer.Elapsed += new ElapsedEventHandler(onCalcTimedEvent);
this._CalcTimer.Interval = 20;

// timer for 1 sec Downtime calculations
this._OneSecDTTimer.Elapsed+=new ElapsedEventHandler(this.oneSecDTEvent);
 
Ad

Advertisements

G

Guest

Thanks, I will break it up and repost

Dale Preston said:
You may want to look your post over, find out what parts of your code really
are involved in causing the problem and add the details of the error
message. Keep it short and it will be much easier to help.

Dale Preston
MCAD, MCDBA, MCSE


FredC said:
OS Name Microsoft Windows XP Professional
Version 5.1.2600 Service Pack 2 Build 2600
Total Physical Memory 1,024.00 MB

MDE 2003 Version 7.1.3008
.NET Framework 1.1 Version 1.1.4322 SP1
Microsoft Visual C# .NET 69462-335-0000007-18707
Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3


Code Description:
The main form does the following:
sets up adata adapter adpTurbine10MinData
Creates and filles two dataset with various unchanging configuration
information
Based on the configuration instantiates:
V47 objects
event handlers to service delegates from each V47 object
Instantiate a 10 minute timer WTGData_10Timer

The event handler WTGDataRowReady10Handler is called by a delegate in the
V47 object every 10
10 minutes. To be fast ( because there could be hundreds of V47 objects) the
handler adds the
the data struct to an collection and returns. Once all the V47 objects add
their data to the collection the timer
WTGData_10Timer is allowed to complete and onCalcTimedEvent is called.

onCalcTimedEvent tests that there are no duplicates in the collection and the
data is added to a data set. Once the data set is complete, it is written to
SQL server.

The "Object reference not set to an instance of an object." occures in
onCalcTimedEvent.

Note that the first 10 minute call works correctly and and the dataset is
written
correctly. All the following 10 minute calls result in a "Object reference
not set to an instance of an object." Error



Code snippets as follows:

//***********************************************************
// Main Form application
//***********************************************************
namespace Trillium.WTGCube
{
public class frmWTGCube : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;

// create a dataset for WTG 10 minute data
private DataSet DS10MinData = new DataSet("DSTurbine10MinData");
private ArrayList WTGData_10 = new ArrayList();


private System.Timers.Timer WTGData_10Timer = new System
.Timers.Timer(); // services 10 minute data write to the SQL Server

public frmWTGCube()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// Constructor code
//

// DataAdapter
SqlDataAdapter adpTurbine10MinData = new SqlDataAdapter(
"SELECT * FROM Turbine10MinuteData", "workstation id=" + WorkstationId +
";packet size=4096;integrated security=SSPI;data source=" +
SqlServerDataSource +
";persist security info=False;initial catalog=TrilliumWind");
try
{
// Set the schema
adpTurbine10MinData.FillSchema(DS10MinData, SchemaType.Source,
"Turbine10MinuteData");

}
catch( SqlException ex)
{
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Can't connect to TrilliumWind Db, SQL Exception -"
+
ex.Message);
}
catch (Exception ex)
{
// catch all other exceptions
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Can't connect to TrilliumWind Db - " +
ex.Message);
}
finally
{
adpTurbine10MinData.Dispose();
}
// create a dataset for WTG Configuration
DataSet DSWTG = new DataSet("WTGConfig");
// create a dataset for Tag Names
DataSet DSTagNames = new DataSet("TagNames");

TrilliumGeneral.ReadWTGConfig(ref DSWTG,
Trillium.Utility.TrilliumConstants.WTGLog,
WorkstationId, SqlServerDataSource );
TrilliumGeneral.ReadTagNames(ref DSTagNames,
Trillium.Utility.TrilliumConstants.WTGLog,
WorkstationId, SqlServerDataSource );

if ( DSWTG == null || DSTagNames == null)
{
// failure to load the data set!!!
WriteToEventLogs.WTGLogWrite( "WindSpace- Abnormal End - Could not build
DataSets");
}

// initialize the WTGs
// for each turbine
foreach ( DataRow TagNameRow in DSTagNames.Tables["TagNames"].Rows )
{
// only one row!
foreach ( DataRow ConfigRow in DSWTG.Tables["TurbineConfiguration"].Rows )
{
switch ( ConfigRow["Turbine Type"].ToString())
{

case "V47":
// instantiate a new turbine
VestasWTG V47 = new VestasWTG( TagNameRow, ConfigRow);

// There are several other delegates instantiated that are not shown

// 10 min datarow delegate
V47.WTGDataRowReady_10 += new
Trillium.WindSpace.VestasWTG.WTGDataRowReady10Delegate(WTGDataRowReady10Hand
ler);

break;
default:
break;
}
}
}

WTGData_10Timer.Elapsed += new ElapsedEventHandler(onCalcTimedEvent); //
handles writing the 10 minute data Collection to the SQL Server
}

private void WTGDataRowReady10Handler(
Trillium.WindSpace.WindSpace.WTGDataGroup_10 myWTGData )
{
WTGData_10Timer.Enabled = false;
try
{
WTGData_10.Add(myWTGData);
}
catch( Exception ex)
{
Utility.WriteToEventLogs.logWrite("WTGDataRowReady10Handler: " +
ex.Message, Utility.TrilliumConstants.WTGLog);
}
WTGData_10Timer.Interval = 9999;
WTGData_10Timer.Enabled = true;

}
public void onCalcTimedEvent(object source, ElapsedEventArgs e)
{
// local storage for each turbines 10 min data
Trillium.WindSpace.WindSpace.WTGDataGroup_10 myWTGData = new
Trillium.WindSpace.WindSpace.WTGDataGroup_10();

WTGData_10Timer.Enabled = false;

// Add each row from the collection WTGData_10 to the data set
// WTGData_10 is the collecttion that has been filled by the event handler
// WTGDataRowReady10Handler. A row for each turbine.

// get a copy of the first row
myWTGData = (Trillium.WindSpace.WindSpace.WTGDataGroup_10) WTGData_10[0];

// add it to the empty DS10MinData
DS10MinData.Clear();
addData(myWTGData);

// add the rest, checking that there are no dups
for(int i = 1; i < WTGData_10.Count; i++)
{
bool bDupFlag = false;
try
{
myWTGData = (Trillium.WindSpace.WindSpace.WTGDataGroup_10)WTGData_10;

foreach ( DataRow DR in DS10MinData.Tables["Turbine10MinuteData"].Rows)
{
//["Date"] = myWTGData.dt;
//["Turbine ID"] = myWTGData.TurbineID;
//["Circuit ID"] = myWTGData.TurbineCircuitID;

// test if myWTGData row equals anything already in the Dataset
DS10MinData
if ( (myWTGData.TurbineID == Convert.ToInt32(DR["Turbine ID"])) &&
(myWTGData.TurbineCircuitID == Convert.ToInt32(DR["Circuit ID"])) &&
(myWTGData.dt == Convert.ToDateTime(DR["Date"])) )
{
//yes this myWTGData has a duplicate in the data set - don't add it
bDupFlag = true;
break;
}
}
}
catch( Exception ex)
{
****ERROR handled here *****
Utility.WriteToEventLogs.logWrite("onCalcTimedEvent: testing for dups
error: " +
ex.Message, Utility.TrilliumConstants.WTGLog);
}
// if this is not a duplicate add it to the Data set
if ( !bDupFlag ) addData(myWTGData);
bDupFlag = false;

// go on to the next element in the collection WTGData_10
}


Utility.WriteToEventLogs.logWrite("onCalcTimedEvent: DataRows in
DS10MinData.Tables: " +
DS10MinData.Tables["Turbine10MinuteData"].Rows.Count.ToString(),
Utility.TrilliumConstants.WTGLog);

// send the dataset to the data base
try
{
#region "SQL stuff"
// code not shown
#endregion

adpTurbine10MinData.Update(DS10MinData);
adpTurbine10MinData.Dispose();
}
catch( SqlException ex)
{
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEventonCalcTimedEvent-Can't write to to TrilliumWind Db,
SQL Exception -" +
ex.Message);
}
catch (Exception ex)
{
// catch all other exceptions
Trillium.Utility.WriteToEventLogs.WTGLogWrite(
"WTGCube-onCalcTimedEvent-Error writing to TrilliumWind Db - " +
ex.Message.ToString());
}

// remove all elements for the DS and the collection
DS10MinData.Clear();
WTGData_10.Clear();
}
private void addData( Trillium.WindSpace.WindSpace.WTGDataGroup_10
myWTGData)
{
// data row names
// ["Wind Speed"]
// ["Wind Direction"]
// ["Power"]
// ["Expected Power"]
// ["Energy"]
// ["Efficiency"]
// ["Status Fault"]
// ["Status Offline"]
// ["Status Running"]
// ["Status Comm Err"]
// ["Status Curtailed"]
// ["Status Eddy Wind"]
// ["DownTime Catagory"]
// ["Communications"]
// ["Events"]
DataRow ROW = DS10MinData.Tables["Turbine10MinuteData"].NewRow();

ROW["Date"] = myWTGData.dt;
ROW["Turbine ID"] = myWTGData.TurbineID;
ROW["Circuit ID"] = myWTGData.TurbineCircuitID;
ROW["Wind Speed"] = myWTGData.WindSpeed_10;
ROW["Wind Direction"] = myWTGData.WindDirr_10;
ROW["Power"] = myWTGData.Power_10;
ROW["Expected Power"] = myWTGData.Expected_Power;
ROW["Energy"] = myWTGData.Energy_10;
ROW["Efficiency"] = myWTGData.Efficiency;
ROW["Status Fault"] = myWTGData.Status_Fault;
ROW["Status Offline"] = myWTGData.Status_Offline;
ROW["Status Running"] = myWTGData.Status_Running;
ROW["Status Comm Err"] = myWTGData.Status_Comm_Err;
ROW["Status Curtailed"] = myWTGData.Status_Curtailed;
ROW["Status Eddy Wind"] = myWTGData.Status_Eddy_Wind;
ROW["DownTime Catagory"]= myWTGData.DownTime_Catagory;
ROW["Communications"] = 0;
ROW["Events"] = 0;

DS10MinData.Tables["Turbine10MinuteData"].Rows.Add(ROW);
}

} // end public class frmWTGCube : System.Windows.Forms.Form

} // end namespace Trillium.WTGCube


namespace Trillium.WindSpace
{

public class WindSpace : IDisposable
{
public WindSpace()
{
}
protected DataSet DSObjInstance = new DataSet();

// Timer for one second calculations
protected Timer _OneSecTimer = new Timer();
protected Timer _OneSecDTTimer = new Timer();

// timer for clock 1 minute and ten minute calculations
protected Timer _CalcTimer = new Timer();

// 10 minute WTG averages and data
public struct WTGDataGroup_10
{
public DateTime dt;
public int TurbineCircuitID;
public int TurbineID;
public string TurbineName;
public float Energy_10;
public float WindSpeed_10;
public float WindDirr_10;
public float Power_10;
public float Efficiency;
public float Expected_Power;
public int Status_Comm_Err;
public int Status_Running;
public int Status_Offline;
public int Status_Fault;
public int Status_Curtailed;
public int Status_Eddy_Wind;
public int DownTime_Catagory;
}
} // end public class WindSpace

public class VestasWTG : WindSpace
{
private bool bCalcFunctionsDone = false;

// one second data refresh
public delegate void WTGDataReadyDelegate(WTGDataGroup w);
public event WTGDataReadyDelegate WTGDataReady;

// one minute data refresh
public delegate void WTGDataReady1Delegate(WTGDataGroup_1 x);
public event WTGDataReady1Delegate WTGDataReady_1;

// ten minute data for SQL Server
public delegate void WTGDataRowReady10Delegate(WTGDataGroup_10 z);
public event WTGDataRowReady10Delegate WTGDataRowReady_10;

public delegate void WTGDTReadyDelegate(DowntimeData v);
public event WTGDTReadyDelegate WTGDTReady;

public VestasWTG(DataRow TagNames, DataRow Configs)
{
//
// Vestas WTG Constructor
//

// timer for 1 sec data and calculations
this._OneSecTimer.Elapsed+=new ElapsedEventHandler(this.oneSecTimerEvent);
this._OneSecTimer.Interval = 999;

// timer for 1 min and 10 min data and calcs
this._CalcTimer.Elapsed += new ElapsedEventHandler(onCalcTimedEvent);
this._CalcTimer.Interval = 20;

// timer for 1 sec Downtime calculations
this._OneSecDTTimer.Elapsed+=new ElapsedEventHandler(this.oneSecDTEvent);

this._OneSecDTTimer.Interval = 999;

//-------------------------------------
// various code
//-------------------------------------

// Enable Calculations
this._OneSecTimer.Enabled = true;
this._CalcTimer.Enabled = true;
this._OneSecDTTimer.Enabled = true;


}

//
// ********************************************************
// onCalcTimedEvent
//
// Functions:
// - call v1MinFunctions - clock 1 minute function
// - calls v10MinFunctions - clock 10 minute functions
//
// - the call occurs on the computers clock minute and 10 minutes
//
//*********************************************************
private void onCalcTimedEvent(object source, ElapsedEventArgs e)
{
DateTime dt = DateTime.Now;

// test if we're at 59.500 sec
if (dt.Second == 59 && dt.Millisecond > 500 )
{
// write out 1 min data
if (!this.bCalcFunctionsDone)
{
this.bCalcFunctionsDone = true;

v1MinFunctions();

// test for ten minute data
if( dt.Minute == 9 ||dt.Minute == 19 || dt.Minute == 29 ||
dt.Minute == 39 || dt.Minute == 49 ||dt.Minute == 59 )
{
v10MinFunctions();
} // end if
}
}
else this.bCalcFunctionsDone = false;
}

// 10 minute functions
public void v10MinFunctions()
{
WTGDataGroup_10 myWTGDataGroup_10 = new WTGDataGroup_10();
float ftemp = 0.0F;
DataRow ROW, READROW;

myWTGDataGroup_10.WindSpeed_10 = 0.0F;
myWTGDataGroup_10.WindDirr_10 = 0.0F;
myWTGDataGroup_10.Power_10 = 0.0F;
myWTGDataGroup_10.Efficiency = 0.0F;
myWTGDataGroup_10.Expected_Power = 0.0F;
myWTGDataGroup_10.Status_Comm_Err = 0;
myWTGDataGroup_10.Status_Running = 0;
myWTGDataGroup_10.Status_Offline = 0;
myWTGDataGroup_10.Status_Fault = 0;
myWTGDataGroup_10.Status_Curtailed = 0;
myWTGDataGroup_10.Status_Eddy_Wind = 0;
myWTGDataGroup_10.DownTime_Catagory = 0;


// Set up Turbine identification in myWTGDataGroup
myWTGDataGroup_10.dt = DateTime.Now;
myWTGDataGroup_10.TurbineCircuitID = _iCircuitID;
myWTGDataGroup_10.TurbineID = _iTurbineID;
myWTGDataGroup_10.TurbineName = _strTurbineName;

//--------------------------------
// do calculations to fill up WTGDataGroup_10
//--------------------------------

// send out 10 minute delegates
// data row delegate
int iRetry = 0;
bool bRetryFlag = false;
string strEx="";
while ( iRetry < 3)
{
try
{
WTGDataRowReady_10(myWTGDataGroup_10);
}
catch( Exception ex)
{
bRetryFlag = true;
strEx = ex.Message;
}
if ( bRetryFlag )
{
iRetry++;
bRetryFlag = false;
}
else break;
}
if ( bRetryFlag )
{
// not successful in sending delegate
Utility.WriteToEventLogs.logWrite("v10MinFunctions: WTGDataRowReady_10
delegate error:" +
strEx.ToString(), Utility.TrilliumConstants.WTGLog);
}

// refresh screen delegate
// WTGDataReady_10(myWTGDataGroup_10);


}

} // End public class VestasWTG


} // end namespace Trillium.WindSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top