Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am having a problem with setting a value of a global object. I am getting the error of "Object reference not set to an instance of an object". Once the frmMain() runs it creates the components, creates the object, and then updates the objects from the UI. I am getting the error when the code goes into the blowerObjectUpdate() and hits the
Global._Blower[i].Model 
The exception is be thrown at this point.

I have put watches on my global object and I can see that the object is being created, but I can set its value. Please offer any suggestions.

C#
using Globals;

namespace WetVacClient
{
   public partial class frmMain : Form
  {
public frmMain()
        {
            InitializeComponent();
            Global.InstantiateBlowerObj();
            blowerObjectUpdate();
        }
 public class Blower
       {
        public bool Valid;
        public string Model;
        public string Series;
        public string Brand;
        public double CFR;
        public double Slip_RPM;
        public double FHP;
        public int Max_RPM;
        public double Pmax;
        public int RPM;
        public double Inlet_Temp;
        public double Inlet_Pressure;
        public double Discharge_Temp;
        public double Discharge_Pressure;
        public double HP;
        public double Torque;
         }
public void blowerObjectUpdate ()     
        {
          for (int i = 1; i < 4; i++)
           int i = 1;
            { 
                switch (i)
                {
                    case 1:
                    case 2:
             Global._Blower[i].Model = comboboxBlower1Model.SelectedItem.ToString();
                    break;
                }
            }
        }
   }
}


namespace Globals
{
  
  
    public static class Global
    {
        public static Blower[] _Blower = new Blower[4];

        public static void InstantiateBlowerObj()
        {
            for (int i = 1; i < 4; i++)
                _Blower[i] = new Blower();
        }
    }
}

FYI I wanted to initialize the object array at 1 instead of 0 to line up with my UI and make things uniform. This is not the problem as I have tried it with i=0 in the for loop.
Posted
Updated 27-Jan-15 9:39am
v7
Comments
Sergey Alexandrovich Kryukov 27-Jan-15 15:24pm    
Instead of emphasizing of the phrase about pulling hair (honestly, not interesting), you should better highlight the line where the exception is thrown, right in code.
—SA
BillWoodruff 28-Jan-15 1:18am    
Glad you found the problem since your code as shown would never compile, and the code as shown would never handle i == #3 ... because you have no "default:" case statement.

I think this code really needs re-architecting. But, it's on you to ask further questions.
Member 11289674 28-Jan-15 9:35am    
Bill this is only a portion of the code I will be using for the whole project. It is being reorganized. I got rid of unnecessary code to simplify and help solve the problem. I appreciate everyone's suggestions.

1 solution

If your location of the line throwing the exception is accurate, it's apparent that in Global._Blower[i].Model, Global._Blower[i] returns null for some i. You further try to dereference this null object by getting .Model, which throws the exception.

[EDIT]
And yes, I did not pay attention that SelectedItem could also be null, with the same effect. It's more important to understand the principles I explained below.
[END EDIT]

That's all. So, what to do? Fix it.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
v4
Comments
Ryan Huseman 27-Jan-15 17:09pm    
I found the problem.
Global._Blower[i].Model = comboboxBlower1Model.SelectedItem.ToString();
should have been
Global._Blower[i].Model = comboboxBlower1Model.SelectedText.ToString();
Sergey Alexandrovich Kryukov 27-Jan-15 17:45pm    
Ah, it could be a problem, too.
—SA
Matt T Heffron 27-Jan-15 20:12pm    
In that case the .ToString() is totally pointless.
The .SelectedText is already a string; and strings are immutable so even if "shared" (a single instance referenced from multiple places) they cannot be modified in such a way as to cause unexpected behavior.
Sergey Alexandrovich Kryukov 27-Jan-15 20:23pm    
Exactly... :-)
—SA

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