Click here to Skip to main content
16,016,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);

           //parameters = new Dictionary<string, Parameter>();
           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;

                                   //Check if 1st root is null
                                   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
                           {
                               // No rows returned, so equipment is unkown or disabled.
                               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;
       }
Posted
Updated 16-Sep-21 0:24am
Comments
Michael_Davies 27-Jan-17 2:38am    
Which line of code gives the error? Have you used the debugger to step thru and check variable?
datt265 27-Jan-17 2:40am    
Hi this is the line of code pg.ParametersGroups.Add(pgName, newPg);

getting null exception can't figure out why
Michael_Davies 27-Jan-17 2:43am    
Have you used the debugger to look at the value in pgName, newPG is okay as you just allocated it, check your SQL dump the response so you can see what is returned and remember values can come back Null from a database and you should always verify their content before using.
datt265 27-Jan-17 2:47am    
Yes hovering over pgName I can get a string value
Michael_Davies 27-Jan-17 2:49am    
pg? parameterGroup?

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
ParameterGroup pg = parameterGroup
if  (!pg.ParametersGroups.TryGetValue(pgName, out pg))
{
      ParameterGroup newPg = new ParameterGroup();
      pg.ParametersGroups.Add(pgName, newPg);  // here pg is always null
}


In the above code, pg will always be null as when something is not found in a dictionary the out parameter will be null.

Thus that code would obviously thrown an exception each time there are no parameter group with the specified name.

Obviously, you should not use the same variable twice if you still need the original dictionary.

I have not check the rest of the code but given that error, I would recommand you to read the documentation of a function before writing code.
 
Share this answer
 
I find its always useful to see if my dictionary is null and then to ensure its initialized as a Dictionary before I try to use Dictionary methods.

Such as:-

// This is what you started with.
Dictionary<string, string=""> dictionary = new Dictionary<string, string="">();

// This is where the problem came from.
dictionary = null;

// This easy statement will ensure your Dictionary functions correctly if it came through as a null and will fix your issue.
if (dictionary == null)
dictionary = new Dictionary<string, string="">();

// You can now use Dictionary methods.
if (!dictionary.ContainsKey("key"))
Console.WriteLine("key");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900