Solved

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

Posted on 2004-08-18
15
307 Views
Last Modified: 2010-04-15
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
Comment
Question by:dean3740
  • 8
  • 7
15 Comments
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838766
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838774
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838793
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
 

Author Comment

by:dean3740
ID: 11838875
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838963
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
 

Author Comment

by:dean3740
ID: 11838974
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
 

Author Comment

by:dean3740
ID: 11839027
I'll post the code.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Expert Comment

by:bigjim2000
ID: 11839033
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
 
LVL 3

Accepted Solution

by:
bigjim2000 earned 500 total points
ID: 11839172
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
 

Author Comment

by:dean3740
ID: 11839173
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11839188
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
 

Author Comment

by:dean3740
ID: 11839197
OK dude. thanks alot!
0
 

Author Comment

by:dean3740
ID: 11848572
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
 

Author Comment

by:dean3740
ID: 11848857
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
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11849910
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now