• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Error:Object reference not set to an instance of an object.

Hello All

I am getting the infamous "Error:Object reference not set to an instance of an object" error for this piece of code:

textNote.Text=((Pref)Prefs.HList["TreatmentPlanNote"]).ValueString;

 And I have no idea why. Here is the full code for the Prefs class in hopes someone may spot my problem. Sorry if a bit long. Thanks in advance.

namespace OpenDental{
      public struct Pref{
            public string PrefName;
            public string ValueString;
      }

      public class Prefs:DataClass{
            public static Hashtable HList;
            public static Pref Cur;

      
      public static bool ConvertDB(){
            ExitApplicationNow ExitApplicationNow2=new ExitApplicationNow();
            ClassConvertDatabase ClassConvertDatabase2=new ClassConvertDatabase();
            if(ClassConvertDatabase2.Convert(((Pref)HList["DataBaseVersion"]).ValueString)){
               return true;
            }
            else{
               MessageBox.Show(Lan.g("Pref","Conversion unsuccessful"));
               ExitApplicationNow2.ExitNow();
               return false;
                }
            }

      public static void Refresh(){
            HList=new Hashtable();
            Pref tempPref = new Pref();
            cmd.CommandText =
                  "SELECT * from preference";
                  FillTable();
                  for(int i=0;i<table.Rows.Count;i++){
                        tempPref.PrefName=PIn.PString(table.Rows[i][0].ToString());
                        tempPref.ValueString=PIn.PString(table.Rows[i][1].ToString());
                        HList.Add(tempPref.PrefName,tempPref);
                  }
            }

            public static bool TryToConnect(){
               try{
                  con.Open();
                  cmd.CommandText="update preference set valuestring = '0' where valuestring = '0'";
                  int rowsUpdated = cmd.ExecuteNonQuery();
                  con.Close();
                                          }
              catch{
                             return false;
               }
                  return true;
            }

         public static bool DBExists(){
            try{
              con.Open();
              con.Close();
                                     }
            catch{
              return false;
            }
               return true;
            }

      public static void UpdateCur(){
            cmd.CommandText = "UPDATE preference SET "
            +"valuestring = '"  +POut.PString(Cur.ValueString)+"'"
            +" WHERE prefname = '"+POut.PString(Cur.PrefName)+"'";
            NonQ(false);
      }

      public static void FlushAndLock(){
            try{
                  con.Open();
                  cmd.CommandText="FLUSH TABLES WITH READ LOCK";
                  int rowsUpdated = cmd.ExecuteNonQuery();
            }
            catch{
                MessageBox.Show(Lan.g("Pref","Error in FlushAndLock"));
                 }
            }

            public static void Unlock(){
                  con.Close();
            }            
      }
0
dean3740
Asked:
dean3740
  • 8
  • 7
1 Solution
 
bigjim2000Commented:
It's not the code for the Pref's class we need, but the code around the place that the error is generated from.

My guess is that either your "TreatmentPlanNote" object does not exist in the collection, or you have not initialized your Pref's object (from which you reference Prefs.Hlist) OR you have you initialized your textNote TextBox.  They must be declared, or else the code won't compile, but if you have not set any of them to some value before you used them, then you will get this error.

-Eric
0
 
bigjim2000Commented:
Oh, one more thing it could be.  You could be retreving the TreatmentPlanNote correctly, but it's member, ValueString, might not be initialized when you try and use it.

When your code breaks, do a Quick Watch of the Perfs object and see if any of the members you are using are null.

-Eric
0
 
bigjim2000Commented:
The more I look at this, the more I think I know where your problem is.

You have the struct:
public struct Pref{
          public string PrefName;
          public string ValueString;
}

you have to be careful when you are using objects in a struct, as they are not expressly initialized.  When you declare this struct, such as by doing:
Pref p;

Make sure the next thing you do is:
p.ValueString = "blah";
p.PrefName = "name";

Otherwise, if you do something like:
Pref p;
string s = p.ValueString;

You will get the exception you are mentioning.

-Eric
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dean3740Author Commented:
Thanks a lot Eric. Let me look more into the code. Your assistance has been wonderful so far. I'll keep you updated shortly unless you see something before i do.

Dean
0
 
bigjim2000Commented:
If you could post all your code from where you declare your Prefs object, to where you get the error, that would be helpful.

-Eric
0
 
dean3740Author Commented:
One thing I probably should've mentioned is that this error didn't arise until i switched my database from mysql to mssql. All was working fine until then but I needed to switch for personal preferences. So I'm not sure why this error is appearing but I do know its in conjunction with the database switch.
0
 
dean3740Author Commented:
I'll post the code.
0
 
bigjim2000Commented:
Well, if the MsSQL database contains some null values, then attempting to use the null values returned might generate that error.  Again, this is all speculation as I have not seen your code.

-Eric
0
 
bigjim2000Commented:
Well, I need to head to bed (but it's only 1:43! *grin* )  I'll take a look at this thread in about 5 mins thogh.  If the code is here, I'll take a quick stab at it, otherwise it'll be on my ToDo list for tomorrow morning ;-)

Things I'd check are:

1) Make sure when you are doing the database query, to check for null values that you store in the ValueString members.
2) If you ARE getting null values, then change the DB design to NOT allow null values in that column.... it makes things safer, and requires you to enter valid data to begin with!  Either that, or specify a default value like "NOT_SET" or something, if you MUST allow null values.
3)  Add a watch on your Perf object, then add a breakpoint when you declare that variable.  Step through your code and watch for null values in your object (especially when you THOUGHT you set that value), and even more especially, when you start to USE that value again!

Hope this helps.

-Eric
0
 
dean3740Author Commented:
I think that is exactly whats happening. I'm really going to have to dig deep into this project. I think I may know where the problem lies. but i tell you what, i'll give you the points because you have been great help with me. Although i'm not sure when I'll have the code posted (for this is a large project but most likely sometime with a few hours or so), just check back with me. I'll keep you posted. Thanks.
0
 
bigjim2000Commented:
Thanks.

If you need any more feedback on this, feel free to ask.  I'm on MSN at schoof_eas@hotmail.com pretty much ALL the time, that is, if I'm not actually... what's the word.... working ;-)

-Eric
0
 
dean3740Author Commented:
OK dude. thanks alot!
0
 
dean3740Author Commented:
Sorry for the delay. There seems to be some other issues with this project that I need to look into. Thanks for all your help however and if I have any further inquiries i'll keep you posted.
0
 
dean3740Author Commented:
BTW Eric, would you mind if i just send you the file to take a look at? There are no errors when you compile but the "Error:Object reference not set to an instance of an object" error is driving me insane. Maybe you can find something that I am overlooking. I would really appreciate it man. You can download the file here http://www.theredn.com (it is a heavy 12mb) and the file is called odsource. Also in the zip is a MSI installer which will give you fulltrust to access the database from the site. You can find it in the setup1 folder, respectively. I don't have msn, however my cousin does have a windows messenger account at t_barrolle@hotmail.com. I'll be on it tonite in case u have any questions. thanks a mil!
0
 
bigjim2000Commented:
In case anyone else was wondering, the error was comming from the MS SQL Database.  Not all the entries from the MySQL Database were being copied.  When we tried to access the index "TreatmentPlanNote" in the Prefs HashTable, it didn't exist, and was thus throwing the exception.

-Eric
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now