I have 2 classes (Parameter & ParameterGroup)
I am getting an null exception on the line when adding to dictionary. pgName is being obtained from a database. Refer to GetParameter method below.
ParameterGroup pg = parameterGroup
if (!pg.ParametersGroups.TryGetValue(pgName, out pg))
{
ParameterGroup newPg = new ParameterGroup();
pg.ParametersGroups.Add(pgName, newPg);
}
What I have tried:
public class ParameterGroup
{
private Dictionary<string, Parameter> _childsGroup;
public Dictionary<string, Parameter> ChildsGroup
{
get { return _childsGroup; }
}
private Dictionary<string, ParameterGroup> _parametersGroup;
public Dictionary<string, ParameterGroup> ParametersGroups
{
get { return _parametersGroup; }
}
public ParameterGroup()
{
_parametersGroup = new Dictionary<string, ParameterGroup>();
_childsGroup = new Dictionary<string, Parameter>();
}
}
public class Parameter
{
private string _parameterName;
public string ParameterName
{
get { return _parameterName; }
}
private double? _nominalValue;
public double? NominalValue
{
get { return _nominalValue; }
}
private double? _upperTolerance;
public double? UpperTolerance
{
get { return _upperTolerance; }
}
private double? _lowerTolerance;
public double? LowerTolerance
{
get { return _lowerTolerance; }
}
public Parameter(string parameterName, double? nominalValue, double? upperTolerance, double? lowerTolerance)
{
_parameterName = parameterName;
_nominalValue = nominalValue;
_upperTolerance = upperTolerance;
_lowerTolerance = lowerTolerance;
}
}
public DBDriverResult GetParameters(string productCode, string processStep, string equipmentType, out ParameterGroup parameterGroup)
{
DBDriverResult retVal = _defaultErrorResult;
logMethodEntry(MethodBase.GetCurrentMethod(), productCode, processStep, equipmentType);
parameterGroup = new ParameterGroup();
using (OracleConnection db = connectToDB())
{
if (db != null)
{
using (OracleCommand cd = db.CreateCommand())
{
cd.CommandType = CommandType.Text;
cd.CommandText = "SELECT * FROM TABLE(APCS.GetParametersByPPE(:p_ProductCode, :p_ProcessStep, :p_EquipmentType))";
cd.Parameters.Add(":p_ProductCode", productCode);
cd.Parameters.Add(":p_ProcessStep", processStep);
cd.Parameters.Add(":p_EquipmentType", equipmentType);
try
{
OracleDataReader r = cd.ExecuteReader();
if (r.HasRows)
{
while (r.Read())
{
ParameterGroup pg = parameterGroup;
string pgName;
if (r["pg1"] != DBNull.Value)
{
pgName = (string)(r["pg1"]);
if (!pg.ParametersGroups.TryGetValue(pgName, out pg))
{
ParameterGroup newPg = new ParameterGroup();
pg.ParametersGroups.Add(pgName, newPg);
}
if (r["pg2"] != DBNull.Value)
{
pgName = (string)(r["pg2"]);
if (!pg.ParametersGroups.TryGetValue(pgName, out pg))
{
ParameterGroup newPg = new ParameterGroup();
pg.ParametersGroups.Add(pgName, newPg);
}
if (r["pg3"] != DBNull.Value)
{
pgName = (string)(r["pg3"]);
if (!pg.ParametersGroups.TryGetValue(pgName, out pg))
{
ParameterGroup newPg = new ParameterGroup();
pg.ParametersGroups.Add(pgName, newPg);
}
}
}
}
string parameterName = (string)(r["ParameterName"]);
double? nominalValue = r["NominalValue"] == DBNull.Value ? null : (double?)r["NominalValue"];
double? upperTolerance = r["UpperTolerance"] == DBNull.Value ? null : (double?)r["UpperTolerance"];
double? lowerTolerance = r["LowerTolerance"] == DBNull.Value ? null : (double?)r["LowerTolerance"];
Parameter p = new Parameter(parameterName, nominalValue, upperTolerance, lowerTolerance);
pg.ChildsGroup.Add(parameterName, p);
}
r.Close();
retVal = DBDriverResult.DefaultSuccess;
}
else
{
retVal = new DBDriverResult(DBDriverResultEnum.ddrRejected, "No Parameters found for Bond Program Recipe.");
}
}
catch (OracleException ex)
{
retVal = standardErrorHandler(ex, MethodBase.GetCurrentMethod().Name);
}
}
}
}
logMethodExit(MethodBase.GetCurrentMethod(), retVal, parameterGroup);
return retVal;
}